R – Overcoming a badly behaving Linux process


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:


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

#start of the 'polling' loop

while [ 1 ]

pgrep $1

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


# 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?