Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
530 views
in Technique[技术] by (71.8m points)

linux - Running PHP script from command line as background process

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...