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: 
```
if ! test -f $PIDFILE || ! psgrep `cat $PIDFILE`; then
    restart_process
    # Write PIDFILE
    echo $! >$PIDFILE
fi

```
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.