0

I have a binary file that stops running every few weeks, you can simply start it again and it runs for another few weeks. The software is not perfect but it would be perfect enough if it restarted automatically.

What is the best way to set something up to automatically check if the binary is running and start it if it is not?

3

1 Answer 1

0

Elegant way of controlling your program is by creating script with PID-file mechanics, that will save program's PID to file, and check with it if process is still running:

#!/bin/bash

PID_FILE=/var/run/rerun.pid # need root permission to store in this directory
EXEC=/usr/bin/gedit # replace it with actual executable

function run() {
    # execute the program
    $EXEC &
    # save its PID
    echo $! > $PID_FILE
}

if [ -e $PID_FILE ]; then
    # check if program is still running
    pid=$(<$PID_FILE)

    # find the proper process
    ps --pid $pid|grep -q `basename $EXEC`

    if [ $? != 0 ]; then
        # not found - rerun
        run
    fi
else
    # no PID file - just execute
    run
fi

Now run the script periodically (eg. in crontab).

Not the answer you're looking for? Browse other questions tagged .