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.
You can simply say:
The way it works is that
grep -qwill 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:
you run the new script in the background and detach it from your console so that your script can keep monitoring
when you run
ps | grepsometimes ps also lists your grep and then the grep “greps itself”, so you have to filter that out.It should look something like this:
or some-such..