Page 1 of 1

Shell script and ps aux

Posted: Mon Jan 16, 2006 9:17 am
by visionmaster
Hello together,

Using screen (http://nerdierthanthou.nfshost.com/2004 ... howto.html) I start several PHP scripts in different screens, which run endlessly.
Sometimes a script crashes. Such scripts should automatically be restarted.

One can check if a specific script is still running, by using 'ps -aux' , displaying running process. If the script is not running anymore, the shell script sould restart the script automatically. How can I check if a specific script is still running and if not, restart the script with /opt/lampp/bin/php my_script.php?

Since I'm a newbie in shell scripting, I would appreciate any helpful, practical suggestions and examples. I don't know where to start off...

Thanks,
visionmaster

Posted: Mon Jan 16, 2006 10:48 am
by Chris Corbyn
Don't write your shell scripts in BASH... you already have PHP under your belt so use that :)

Code: Select all

#!/usr/bin/php
<?php

$command = '/usr/bin/php file.php';
$data = `ps aux | grep $command`;
if (empty($data)) echo "Process dead\n";
else echo "It's alive!!!";

?>
CHMOD the file to 0770 and it should be executable like a bash script ;)

Posted: Mon Jan 16, 2006 12:08 pm
by timvw
The common approach is to write the PID to a file when the application starts (pidof $0)
And later on you can see if the pid is in the processlist ( ps aux | awk '{ print $2 }' | grep $pid | grep $pid | wc -l should equal 1 )