How do I write a bash script to restart a process if it dies?

后端 未结 8 2117
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:47

I have a python script that\'ll be checking a queue and performing an action on each item:

# checkqueue.py
while True:
  check_queue()
  do_something()
         


        
8条回答
  •  鱼传尺愫
    2020-11-22 03:19

    I've used the following script with great success on numerous servers:

    pid=`jps -v | grep $INSTALLATION | awk '{print $1}'`
    echo $INSTALLATION found at PID $pid 
    while [ -e /proc/$pid ]; do sleep 0.1; done
    

    notes:

    • It's looking for a java process, so I can use jps, this is much more consistent across distributions than ps
    • $INSTALLATION contains enough of the process path that's it's totally unambiguous
    • Use sleep while waiting for the process to die, avoid hogging resources :)

    This script is actually used to shut down a running instance of tomcat, which I want to shut down (and wait for) at the command line, so launching it as a child process simply isn't an option for me.

提交回复
热议问题