Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is it possible to configure my CentOS VPS to continually check to make sure that a php script is running? Currently I'm using the screen php method which works fine, but if it gets terminated somehow, how can I start it backup?

I tried adding the command to /etc/rc.d/rc.local, however this morning I checked, and the script wasn't running.

share|improve this question

4 Answers 4

This task is well suited to a process monitor like Monit.

The monitoring configuration will be made much easier with Monit if your PHP process produces a PID file. An article describing how to use Monit to check and start PHP processes is available here.

share|improve this answer
    
This link seems to be dead now. –  Gaurav Gupta Apr 28 at 6:41
    
@GauravGupta Archive.org can usually help you out. https://web.archive.org/web/20100615000000*/http://bipinb.com/monitoring-a-daem‌​on.htm –  ceejayoz Apr 28 at 13:57
    
True, already checked that. "Wayback Machine doesn't have that page archived." –  Gaurav Gupta Apr 29 at 4:25
    
@GauravGupta It works fine. SO's adding a space in the URL though - doesn't like the *. –  ceejayoz Apr 29 at 5:24

You can write a checking script that parses the output of the ps command, looking for your php instance. If it exists, then your script should silently exit; if it does not, then your script should launch your php program, and then write a message to stdout warning that the process was dead and had to be restarted.

Then, add your checking script to your crontab (e.g.)

MAILTO=you@domain.com
*/5 * * * * /path/to/checking-script.sh

In this example, your checking script runs every 5 minutes. If the php process is dead and your checking script relaunches it, then it will also output the formentioned message which will be emailed to you (as an alert).

share|improve this answer

This task is well suited for some outside monitoring like AlertFox, Pingdom, Monitis,... (all these have free plans).

The approach is always the same: you create a PHP page that checks that the PHO script is running, and the monitoring service calls it periodically:

http://blog.alertfox.com/2011/01/monitoring-disk-space-and-other-status.html

share|improve this answer

you can use supervisord for this purpose. It is a python application but very simple to setup. Supervisord provides a auto restart directive in its config which will restart the script is case it dies.

you can read more about it on http://phpadvent.org/2009/daemonize-your-php-by-sean-coates

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.