1

I would like to have a shell scipt that runs infinitely and keeps checking status of a php script (say my.php) and restarts it if the script has terminated somehow. I have the idea to go for -

ps -aux | grep "my.php"

and then use the result of this to check the status and do accordingly. Thanks in advance.

1
  • 1
    if your system has pgrep, it's a whole lot easier to use. Commented Jan 6, 2011 at 19:50

3 Answers 3

3

You can simply say:

ps -aux | grep -q "my.php" || php -f my.php

The way it works is that grep -q will not output anything but will return an "OK" exit code if it found something. when it returns a "NOT OK" exit code, the part after the || ("or") gets executed (because of boolean short-circuit evaluation - look it up).

You also need to make sure that:

  1. you run the new script in the background and detach it from your console so that your script can keep monitoring

  2. when you run ps | grep sometimes ps also lists your grep and then the grep "greps itself", so you have to filter that out.

It should look something like this:

while true
    ps -aux | grep -v grep | grep -q "my.php" || ( nohup php -f "my.php" & )
    sleep 1
done

or some-such..

Sign up to request clarification or add additional context in comments.

1 Comment

"T" means that someone sent it a -STOP signal - this usually happens on purpose: the script was suspended in the background, or is being debugged. You don't want to go and start a second process when that happens, because the script is liable to continue in a moment.
1

Another approach is, start your php-program in a loop:

for ((;;))
do
   my.php 
done

With Linux ps, you could use

ps -C "my.php"

instead of grep, to identify my.php. Grep commands often find themselves. Maybe your ps has a similar switch?

4 Comments

I don't know what operating system you're using, but in mine, -C is "Change the way the CPU percentage is calculated by using a "raw" CPU calculation that ignores "resident" time". If the OP's question is about PHP and Shell, answer the question from the perspective of PHP and shell, not your particular flavour of some other command.
I'm using Linux, and the manpage of ps says This ps conforms to: 1) Version 2 of the Single Unix Specification, 2) The Open Group Technical Standard Base Specifications, Issue 6, 3) IEEE Std 1003.1, 2004 Edition, 4) X/Open System Interfaces Extension [UP XSI], 5) ISO/IEC 9945:2003 - I thought that's enough.
All we know from the question is that the OP's operating system uses Berkeley-style options for ps rather than SystemV options.
You're right. I deemphasized the Linux style ps-part, and concentrate more on the bash-part. Thanks.
1

If you DO really feel the need to grep the output of ps, beware of your grep finding itself.

[ghoti@pc ~]$ sleep 60 &
[1] 66677
[ghoti@pc ~]$ ps aux | grep sleep
ghoti      66677   0.0  0.0   3928    784  11  S     4:11PM     0:00.00 sleep 60
ghoti      66681   0.0  0.0  16440   1348  11  S+    4:12PM     0:00.00 grep sleep
[ghoti@pc ~]$

There's an easy way to avoid this. Just make part of your grep into a more complex regular expression.

[ghoti@pc ~]$ sleep 60 &
[2] 66717
[ghoti@pc ~]$ ps aux | grep '[s]leep'
ghoti      66677   0.0  0.0   3928    784  11  S     4:11PM     0:00.00 sleep 60
ghoti      66717   0.0  0.0   3928    784  11  S     4:13PM     0:00.00 sleep 60
[ghoti@pc ~]$ 

On the other hand, if you just want to make sure that your PHP script always runs, you can wrap it in something that re-runs it when it dies:

while true; do
  php /path/to/my.php
done

If you want this to run at startup, you can edit your crontab on the server, and use a @reboot tag, assuming you're using "Vixie" cron (common on Linux and BSD):

@reboot /path/to/wrapperscript

You can man crontab and man 5 crontab for more details on how to use cron and the @reboot tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.