views:

1558

answers:

6

I have a PHP script that listens on a queue. Theoretically, it's never supposed to die. Is there something to check if it's still running? Something like Ruby's God ( http://god.rubyforge.org/ ) for PHP?

God is language agnostic but it would be nice to have a solution that works on windows as well.

+3  A: 

Simple bash script

#!/bin/bash
while [true]; do
    if ! pidof -x script.php;
    then
     php script.php &
    fi
done
Mez
this wouldn't work on windows though...
Eric Lamb
Also, watch your CPU usage skyrocket :)
broady
not really broady... I use this quite often (well, ok, on a cron job every minute)
Mez
A: 

One possible solution is to have it listen on a port using the socket functions. You can check that the socket is still listening with a simple script. Even a monitoring service like pingdom could monitor its status. If it dies, the socket is no longer listening.

Plenty of solutions.. Good luck.

DreamWerx
+1  A: 

Not for windows, but...

I've got a couple of long-running PHP scripts, that have a shell script wrapping it. You can optionally return a value from the script that will be checked in the shell-script to exit, restart immediately, or sleep for a few seconds -and then restart.

Here's a simple one that just keeps running the PHP script till it's manually stopped.

#!/bin/bash
clear
date
php -f cli-SCRIPT.php
echo "wait a little while ..."; sleep 10
exec $0

The "exec $0" restarts the script, without creating a sub-process that will have to unravel later (and take up resources in the meantime). This bash script wraps a mail-sender, so it's not a problem if it exits and pauses for a moment.

Alister Bulman
This is pretty much the same as the God script I'm currently using and definitely better than busy-waiting.
+2  A: 

Just append a second command after the script. When/if it stops, the second command is invoked. Eg.:

php daemon.php 2>&1 | mail -s "Daemon stopped" [email protected]

Edit:

Technically, this invokes the mailer right away, but only completes the command when the php script ends. Doing this captures the output of the php-script and includes in the mail body, which can be useful for debugging what caused the script to halt.

troelskn
No, during the running of php, the mail command is ran. But, once it stops, it sends an EOF, which causes the mailer to send the mail
Mez
Thanks for pointing it out Mez. I'll edit the snippet.
troelskn
A: 

If you have your hands on the script, you can just ask him to set a time value every X times in db, and then let a cron job check if that value is up to date.

e-satis
A: 

troelskn wrote:

Just append a second command after the script. When/if it stops, the second command is invoked. Eg.:

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

This will call mail each time a line is printed in daemon.php (which should be never, but still.)

Instead, use the double ampersand operator to separate the commands, i.e.

php daemon.php & mail -s "Daemon stopped" [email protected]
Daniel Schierbeck
Mez