Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I use Gnome Do as a launcher for most things. I have it set to run on login and for most of the time it sits in the background waiting for me to mash a keyboard shortcut. About once a week, it falls over. I mash my shortcut and nothing happens. I have to re-launch it.

It's not amazingly annoying but I would like to know if there are any good ways to make sure desktop applications keep running forever. If they die, they get re-launched (perhaps with a crash timeout so if it crashes on load, it doesn't keep trying to run it over and over and over again).

I know some applications do this. On Gnome2, if gnome-panel isn't running, the system forces it to load. Is there a simple way of tapping into this logic (of course, without breaking Gnome).

I don't want this question to be too specific to Gnome Do or even Gnome because I have more than one application for a solution (I run Boxee on an OpenBox desktop on our media centre) and I'm sure other people all have their own issues they'd like to fix through something like this.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Another not so elegant solution:

#!/bin/bash

threshold=2
program="$1"

while true; do
    start=$(date +%s)
    "$program"
    ret_val=$?
    stop=$(date +%s)
    if [[ ret_val -eq 0 ]]; then
        # normal exit, quit
        exit 0
    else
        # abnormal exit, check runtime. if < threshold quit
        runtime=$((stop - start))
        if [[ runtime -lt $threshold ]]; then
            echo "crash on start"
            exit 1       
        fi
    fi
done

This will restart, if the program (first argument) exits with a return code != 0 and ran at least threshold seconds.

I tried to create an Upstart job using the respawn and respawn limit options, but that somehow failed.

share|improve this answer
    
I guess it's a bit complicated to add a correct $DISPLAY value to the upstart job... –  JanC Jun 6 '11 at 16:55
    
Actually, it should be possible if you always start the job from inside X, and pass $DISPLAY as a parameter. –  JanC Jun 6 '11 at 16:56
    
@JanC I tried and I failed. ;-) –  htorque Jun 6 '11 at 21:30

A simple, if not particularly elegant, solution would be to launch the application from a script on loop, such that

#!/bin/bash

while true
 do
    gnome-do # or application of choice
    sleep 10 # prevent super-spawn rates
done

Although this may have to be checked per application, as bash does not halt for all programs...

You could also adapt it to check for errorcodes, so it only respawns if the application crashes, rather than closed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.