tags:

views:

46

answers:

2

I read this Question: "How do I write a bash script to restart a process if it dies".

A solution is to:

until myserver; do
    echo "Server 'myserver' crashed with exit code $?.  Respawning.." >&2
    sleep 1
done

Will this work for Tomcat/Jetty processes that are started from a script?

Do I test the success with "kill" to see if the process restarts?

A: 
while true
do
  if pgrep jett[y] 1>/dev/null;then 
   sleep 1
  else
   # restart your program here
  fi

done
ghostdog74
Thanks. I this doesn't detect any process. Also I am really keen to know if the initial post works.
Zlowrider
A: 

If the script returns exit codes as specified in the answer at that link, then it should work. If you go back and read that answer again, it implies that you should not use kill. Using until will test for startup because a failed startup should return a non-zero exit code. Replace "myserver" with the name of your script.

Your script can have traps that handle various signals and other conditions. Those trap handlers can set appropriate exit codes.

Here is a demo. The subshell (echo "running 'dummy'"; sleep 2; exit $result) is a standin for your script:

result=0
until (echo "running 'dummy'"; sleep 2; exit $result)
do
    echo "Server 'dummy' crashed with exit code $?.  Respawning.." >&2
    sleep 1
done

Try it with a failing "dummy" by setting result=1 and running the until loop again.

Dennis Williamson