1

i am using shell script to monitor the working of a php script. My aim is that this php script should not sleep / terminated and must always be running.The code i used is -

ps aux | grep -v grep | grep -q $file || ( nohup php -f $file -print > /var/log/file.log & ) 

now this idea would not work for cases if the php script got terminated(process status code T). Any idea to handle that case. can such processes be killed permanently and then restarted.

3
  • What is your PHP script doing that it must always be running? Wouldn't it be simpler to use a different language? Commented Jan 24, 2011 at 9:14
  • 1
    @amphetamachine, I'm all in favor of people moving away from PHP, but the problem would be the same in almost any target language. :) Commented Jan 24, 2011 at 9:19
  • Please see Process Management. Commented Jan 24, 2011 at 17:14

2 Answers 2

2

How about just restarting the php interpreter when it dies?

while true ; do php -f $file -print >> /var/log/file.log ; done

Of course, someone could send the script a SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU to cause it to hang, but perhaps that person has a really good reason. You can block them all except SIGSTOP, so maybe that's alright.

Or if the script does something like call read(2) on a device or socket that will never return, this won't really ensure the 'liveness' of your script. (But then you'd use non-blocking IO to prevent this situation, so that's covered.)

Oh yes, you could also stuff it into your /etc/inittab. But I'm not giving you more than a hint about this one, because I think it is probably a bad idea.

And there are many similar tools that already exist: daemontools and Linux Heartbeat are the first two to come to mind.

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

3 Comments

If you're going to be sending IPC signals, don't forget about PHP's ability to trap them.
that i am already doing by running this script through a cron job. My doubt is though how to handle cases where the process gets a T status code.??/
@ayush, as amphetamachine mentions, you can block or ignore SIGTSTP, SIGTTIN, and SIGTTOU. You cannot block or ignore SIGSTOP, but if someone is sending the process SIGSTOP there must be a good reason. You can also drastically reduce the chances of someone tracing your script and thus causing it to sleep: wiki.ubuntu.com/SecurityTeam/Roadmap/KernelHardening#ptrace
1

If the script is exiting after it's been terminater, or if it crashes out, and needs to be restarted, a simple shell script can take care of that.

#!/bin/bash
# runScript.sh - keep a php script running
php -q -f ./cli-script.php -- $@
exec $0 $@;

The exec $0 re-runs the shell script, with the parameters it was given.

To run in the background you can nohup runScript.sh or run it via init.d scripts, upstart, runit or supervisord, among others.

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.