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: 
In-line:
--------

```bash
while true; do <your-bash-snippet> && break; done

```

This will restart continuously `<your-bash-snippet>` if it fails: `&& break` will stop the loop if `<your-bash-snippet>` stop gracefully (return code `0`).

To restart `<your-bash-snippet>` in all cases:

```bash
while true; do <your-bash-snippet>; done

```

**e.g. #1**

```bash
while true; do openconnect x.x.x.x:xxxx && break; done

```

**e.g. #2**

```bash
while true; do docker logs -f container-name; sleep 2; done

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