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

后端 未结 8 2095
-上瘾入骨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:29

    I'm not sure how portable it is across operating systems, but you might check if your system contains the 'run-one' command, i.e. "man run-one". Specifically, this set of commands includes 'run-one-constantly', which seems to be exactly what is needed.

    From man page:

    run-one-constantly COMMAND [ARGS]

    Note: obviously this could be called from within your script, but also it removes the need for having a script at all.

    0 讨论(0)
  • 2020-11-22 03:32

    Have a look at monit (http://mmonit.com/monit/). It handles start, stop and restart of your script and can do health checks plus restarts if necessary.

    Or do a simple script:

    while true
    do
    /your/script
    sleep 1
    done
    
    0 讨论(0)
提交回复
热议问题