views:

86

answers:

4
+2  Q: 

Monitor a process

Is there a way I a process can be monitor and if it were to die, can a script be called which in turn will bring the process back up ?

A: 

I wrote one of these a while ago called relight. There also exist more robust solutions, but this one is simple and works for me.

Greg Hewgill
+1  A: 

monit.

http://mmonit.com/monit/

thanks :) looks like it will be perfect for me.
Prakash Raman
+1  A: 
#!/bin/bash
while true
do
  if pgrep myprocess  >/dev/null ;then
     echo "up"
  else
     echo "down"
     /path/to/myprocess &
  fi
  sleep 60
done

or you could remove the while loop and sleep and put the script in a cron job set to run every minute

ghostdog74
A: 

The easiest thing to do is have the initial parent do the monitoring. EG,

#!/bin/sh

while true; do
  cmd
  # When you get here the process has died.  start
  # the loop again and restart it
done

This script is liable to be killed so you might want to trap signals, but the same will be true of any monitor that you might write. You will probably also want to insert a delay if cmd is terminating immediately, or add some logging (call logger after you call cmd). There's no need to get fancy.

William Pursell