Question

What is the best way to keep a PHP script running as a daemon, and what's the best way to check if needs restarting.

I have some scripts that need to run 24/7 and for the most part I can run them using nohup. But if they go down, what's the best way to monitor it so it can be automatically restarted?

Was it helpful?

Solution

If you can't use the (proper) init structure to do this (you're on shared hosting, etc.), use cron to run a script (it can be written in whatever language you like) every few minutes that checks to see if they're running, and restarts them if necessary.

OTHER TIPS

We run our daemons by piping the output to mail.

php daemon.php | mail -s "daemon stopped" foo@example.org

That way, when/if the daemon stops, it will send a mail, and we will be notified that way.

It still means manual restart of the daemons of course, but we'll know right away. Usually, if the daemons stopped, it means that there is something else that needs to be taken care of anyway, so that's usually ok.

Quick and dirty cron to restart your daemon:

* * * * * USER ps auxww | grep SCRIPTNAME > /dev/null || SCRIPTNAME

Replace USER with the user that the daemon runs as and SCRIPTNAME with the name of your script. Stick this in /etc/cron.d/restart_php_daemon. It should run every minute. Change the first * to */2 or */5 to run less frequently.

UPDATE

If you're putting this into your own crontab:

Run crontab -e and add:

* * * * * ps auxwww | grep SCRIPTNAME > /dev/null || SCRIPTNAME

The most elegant solution is phpdaemon or reactPHP.

I've had success with running a wget and sending the result to /dev/null on a shared server.

Daemon is a linux process that runs in background; apache or mysql are daemons. In a linux environment, we can run a background program using cronjob, but it has some limitations, and in some scenarios it' s not a good idea. For example, using cronjob, we can't control if the previously run has finished yet. So often it's more convenient run a process as a daemon.

// Daemonize
$pid = pcntl_fork(); // parent gets the child PID and child gets 0
if($pid){ // if pid is not 0
     // Only the parent will know the PID. Kids aren't self-aware
     // Parent says goodbye!
     print "Parent : " . getmypid() . " exiting\n";
     exit();
}
print "Child : " . getmypid() . "\n";

The code above is taken from very good article about how to create a daemon in php. You can read this at link

I use a PHP-based script to read from a database and send emails out (using the PEAR Mail_Queue library). I run it from within a bash script and based on the returned result (from "exit $status;") either halt, sleep X seconds, or immediately restart. (I also put a check of the load average/sleep into the PHP script to avoid stressing the mail system).

If it was for a long-term daemon that had to be continually running, then I agree, it probably would not be the best thing to run this (though I have heard of some socket servers that did run successfully long term), however, PHP 5.3 does also now have improved garbage collection, and if the script is well written enough to not exit unplanned, then memory should be far less of a problem that before.

I agree that PHP is not the best tool for this, however I can understand why you want to use PHP so you can reuse components from your application such as database access, and so on.

I had a similar problem and I ended up developing The Fat Controller which is a daemon written in C that can run PHP scripts. It can also run as a multithreaded daemon, running many instances of a script in parallel.

There's more information and use cases here: http://www.4pmp.com/fatcontroller/

TBH, PHP probably isn't the best tool for this, really not what it was designed for. I've heard of memory leaks and other bad things happening when you try this. Also bear in mind PHP only has a finite amount of resource ids (for file handles, db connections ect) per execution of a script.

Be better of using something else, maybe python or perl, though I don't have any real experience writing these sorts of apps, but I do know PHP isn't right for what your trying to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top