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

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2009-03-30T11:27:08Z Indexed on 2010/05/24 9:51 UTC
Read the original article Hit count: 195

Filed under:
|
|

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()

How do I write a bash script that will check if it's running, and if not, start it. Roughly the following pseudo code (or maybe it should do something like ps | grep?):

# keepalivescript.sh
if processidfile exists:
  if processid is running:
     exit, all ok

run checkqueue.py
write processid to processidfile

I'll call that from a crontab:

# crontab
*/5 * * * * /path/to/keepalivescript.sh

Thanks in advance.

© Stack Overflow or respective owner

Related posts about bash

Related posts about script