Use until the process restarts when it dies

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

Decision:

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

Will this work for Tomcat / Jetty processes that start with a script?

I check success with "kill" to see if the process is restarted?

+4
source share
2 answers

If the script returns the exit codes indicated in the answer by this link , then it should work. If you come back and read this answer again, it means you should not use kill . Using until will be checked for start because a failed start should return a non-zero exit code. Replace "myserver" with the name of your script.

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

Here is a demo. The subshell (echo "running 'dummy'"; sleep 2; exit $result) is 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 to crash "dummy" by setting result=1 and running the until loop again.

+3
source
 while true do if pgrep jett[y] 1>/dev/null;then sleep 1 else # restart your program here fi done 
+2
source

All Articles