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: 
```sh
watch "yourcommand"

```

It will restart the process if/when it stops (after a 2s delay).

```sh
watch -n 0.1 "yourcommand"

```

To restart it after 0.1s instead of the default 2 seconds

```sh
watch -e "yourcommand"

```

To stop restarts if the program exits with an error.

Advantages:
-----------

* built-in command
* one line
* easy to use and remember.

Drawbacks:
----------

* Only display the result of the command on the screen once it's finished
I use this for my npm Process

```
#!/bin/bash
for (( ; ; ))
do
date +"%T"
echo Start Process
cd /toFolder
sudo process
date +"%T"
echo Crash
sleep 1
done

```