Shell script and ps aux

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Shell script and ps aux

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 )
Post Reply