Question: 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

```

Answer: 
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

```
You should use monit, a standard unix tool that can monitor different things on the system and react accordingly.

From the docs: <http://mmonit.com/monit/documentation/monit.html#pid_testing>

```

check process checkqueue.py with pidfile /var/run/checkqueue.pid
       if changed pid then exec "checkqueue_restart.sh"

```

You can also configure monit to email you when it does do a restart.