37

I'm trying to run a PHP script continually in the background via the command line in Linux. I have tried the command php filename.php & but it seems like the script execution terminates very quickly, while it should keep running until the process is terminated.

Any suggestions?

5
  • 2
    What do you mean it terminates quickly? The & at the end will give you your prompt back to continue executing shell commands while your script runs in the background.. You can see if it is still there running by typing "jobs" Commented Aug 8, 2011 at 22:06
  • 1
    did you try setting error_reporting(E_ALL) and setting display_errors=1? there might some errors and your process is maybe terminating abnormally Commented Aug 8, 2011 at 22:14
  • 2
    try: exec("nohup php file.php >/dev/null 2>&1 &"); Commented Aug 8, 2011 at 22:19
  • @Zak: I can execute the command that I mentioned, and then it will return. If I run jobs afterward, it lists the process but says that it has stopped. Commented Aug 8, 2011 at 22:51
  • If I run it without the & flag, then it will continue running without a problem until I disconnect from the server, but I'd like to run it constantly in the background 24/7. Commented Aug 8, 2011 at 22:55

4 Answers 4

65

Are you sure the script doesn't contain any errors? This is what normally makes "execution terminates very quickly".
First, append:

error_reporting(E_ALL); ini_set('display_errors', 1);

at the top of your script to display any errors it may have, then you can use :

nohup php filename.php &

nohup runs a command even if the session is disconnected or the user logs out.

OR

nohup php filename.php >/dev/null 2>&1 &

Same as above but doesn't create nohup.out file.


You can also use:
ignore_user_abort(1);

Set whether a client disconnect should abort script execution


`set_time_limit(0);`

Limits the script maximum execution time, in this case it will run until the process finishes or the apache process restarts.


#Notes The php and the filename.php paths may be provided as a full-path, instead of php and filename.php, you can use /usr/bin/php and /full/path/to/filename.php.
Full Path is recommended to avoid file not found errors.

Sign up to request clarification or add additional context in comments.

1 Comment

Would echo statements cause the script to terminate if there is no output redirection?
17

the process may be closed when your session is closed.

try using nohup php filename.php

Comments

7
nohup php file.php > /dev/null 2>&1 &

The greater-thans (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected to /dev/null, and something is being redirected to &1

Standard in, out, and error

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

the command above is redirecting the standard output to /dev/null, which is a place you can dump anything you don’t want, then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!

& at the end puts the command in background.

ref: https://www.xaprb.com/blog/2006/06/06/what-does-devnull-21-mean/

Comments

3
nohup /path/to/command-name arg1 arg2 &

Where:
command-name : name of shell script or command name. You can pass argument to command or a shell script.
& : nohup does not automatically put the command it runs in the background; you must do that explicitly, by ending the command line with an & symbol.

1 Comment

Duplicate of answer. And use code formatting, please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.