A bit about the stuff I've done

Tuesday, 16 December 2014

Running something "all tihe time" (as a normal user)

 N.B. the following is not a replacement for proper services, the use-case is slightly different.

So, you have an unstable application. But you want it to be running all the time, even when you are not there to restart it after a crash (or a reboot).

You could use cron, but that will start a new instance of the app every single time. Hardly ideal!

Well actually the solution IS to use cron but not to start the app directly.

Below is a small script I wrote which runs a whole bunch of applications in the background and automagically restarts them if the crash.
Put small scripts into the ~/screens folder and this script will make sure each of them is running, and start it if not.

ls ~/screens -l  | grep "^...x" | sed -r "s/.*?:[0-9][0-9]\ //"  | sed -r "s/.*?[A-Za-z]* *[0-9]* *[0-9]{4} //"|  grep -v "^~" | grep -v "^#" | ( while read f
        echo $f
        name=AS_$(cat $f | grep -i "^[ \t]*#[ \t]*name:" | sed "s/[^:]*:[ \t]*//")
        desc=$(cat $f | grep -i "^[ \t]*#[ \t]*desc(ription)?:" | sed "s/[^:]*:[ \t]*//")

        echo -en "Checking $desc ($name) ... "
        if [ $( screen -ls | grep -i $name | wc -l)  == 1 ]
                echo "already running"
                echo -en " starting ..."
                screen -dmS $name -s $f && echo "done" || echo "failed"

Set this script to run as often as you like (I have mine set at 5 minutes) using crontab
#crontab -e
*/5 * * * * /home/lee/screens2.sh >/dev/null 2>/dev/null

Job Done!

No comments:

Post a Comment