R – Overcoming a badly behaving Linux process

bashlinuxscripting

I have a badly behaving process (launched via a user command) which keeps dying at erratic intervals, and I need it to stay alive till I manually kill it. Here is my straight, but probably stupid solution:

#!/bin/bash

if [ -z $1 ]
then
 echo "Usage: /s98ize.sh <process name>"
 exit
fi

#start of the 'polling' loop

while [ 1 ]
do

pgrep $1

if [ $? -eq 0 ]
then
 echo "Already running"
else
 # If process has died or not started, start it
 $1
 # FIXME: I have not done any error checking this script will not catch a
 # unavailable command
fi

done

# end of the polling loop

The gist is: if the above process is running, then do 'nothing', else launch it. A very straightforward disadvantage is that it keeps 'polling'. However, it serves my purpose.

As I write this, I think I can do a signal handling on the process, so that once it gets the kill signal, I can restart it? What do you think?

Best Solution

Have you considered djb's supervise program? It does exactly this: run a program, restart it if it exits, provide a means to control it, etc?