3

I need to keep a php script running and alive on my server, the script runs and checks a DB for record, processes if needed, sleeps for 3 and then loops to the top of the script in an infinite loop. The issue is launching it, if I launch it via terminal (its running on an ubuntu system) using php script.php then if the terminal session is ended the script stops running.

So how can I go about launching the script so that it will remain running in the background.

Furthermore if I set up a cron job that runs once an hour and fires off a different script that check the primary one is still running and if not restarts it, how would I get the this checker script to check that the initial script is still running (even if its in sleep).

Any assistance will be greatly appreciated

David
  • 3,927
  • 6
  • 30
  • 48
  • `supervisord` can do this for you, but just running as a daemon works as well, creating an `init.d` script with LSB style headers, possibly let it be watched by `monit` instead of some custom script. – Wrikken Apr 24 '13 at 21:34
  • You'll probably find my answer the easiest. :P – Glitch Desire Apr 24 '13 at 21:53

3 Answers3

4

If starting the script from the web is an option, then set time limit to "unlimited" (in the script itself):

set_time_limit(0); 

and set ignore_user_abort to "true":

ignore_user_abort(true);

Then you can run the script as usual from the web, and it will run forever (until the process is killed or script exits in the usual way).

Of course, you can (and MUST) protect such a starter-script by password, e.g. by using HTTP authentication via .htaccess, so that somebody cannot start many forever-running scripts, which would lay down your server.


On checking whether another process is running, see question1, question2, or try searching "[php] check if process is running" here on StackOverflow. See also http://php.net/manual/en/refs.fileprocess.process.php.

Community
  • 1
  • 1
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
2

If you want to run it from the terminal and keep it running permanently, look into GNU screen. It's a virtual terminal that keeps running in the background even when you close the terminal.

$ sudo apt-get install screen

With this, you can simply do:

$ screen php myscript.php

The script will load in a screen session which you can disconnect from, and even when you close the terminal it will keep running. To check up on it, simply call:

$ screen -x

Best part is screen is free software and is in the repo of all good distros (and other *NIX's if Linux doesn't float your boat).

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
  • 1
    Why not just use `nohup php myscript.php &` - `nohup` to continue running after disconnect, `&` to run the command in the background and release control of the terminal... – Ben Swinburne Apr 24 '13 at 22:01
  • `nohup` can do this, but `screen` has the advantage of being able to be reconnected. `nohup` process would have to be killed via `pkill` if it hangs too whereas `screen` can be killed via `screen` itself. – Glitch Desire Apr 24 '13 at 22:03
  • If the idea is to run the process all the time, surely the means by which to kill it is irrelevant, and to access the script by screen is no different to tailing a log file which can be limited to a maximum size if desired – Ben Swinburne Apr 24 '13 at 22:06
  • Thank you for your reply, it is very useful to know and is a solution to the problem, however I do want to keep it contained within PHP. – David Apr 24 '13 at 22:13
  • `exec('screen php myscript.php');` :P – Glitch Desire Apr 24 '13 at 22:14
1

Cron Job would be one solution: More details about Cron job.

Another way to do it is to use Gearman or some other taks managers like in this post

Community
  • 1
  • 1
Mehdi Karamosly
  • 5,388
  • 2
  • 32
  • 50