tags:

views:

2210

answers:

7

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?

+6  A: 

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.

sysrqb
Can you elaborate on what "use[ing] the (proper) init structure to do this" is and how this would solve the issue of ensuring the script is running?
Jonah Braun
A: 

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.

+1  A: 

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

Daniel
+2  A: 

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
Gary Richardson
This looks like a really handy idiom, but I'm not sure this works on mac osx and/or if you don't have root access. Do you have an equivalent for an entry in a user's own crontab file?
dreeves
A: 

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.

Alister Bulman
A: 

We run our daemons by piping the output to mail.

php daemon.php | mail -s "daemon stopped" [email protected]

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.

troelskn
A: 

@rmbarnes, about memory leaks:

I've implemented a performance monitor (generates rrd files from various sources like system load, cpu usage, disk space, etc) as a php daemon which uses the pcntl_* functions to fork itself into background (and an initrd script which starts it).

it runs since 400 days ago (php 4.3.9) and I don't notice any memory usage problems.

so, bottom line: it can be done. (with some minor care about resources used, of course).