0

I have struggled with this problem for three days and I have tried many ways to solve it but not successful yet. I hope you guys can help me...

I have an GUI application. I want to start this application automatically. And when it goes down or closed unexpectedly, I want to reopen this application.

I tried to use upstart script, however although there is no problem about services with upstart, GUI application is not starting with upstart script. It says cannot connect X server..

Should I add or change some settings to open with upstart or is there any way to open GUI application automatically when unexpected exit or shut down occurs (not just once after login I mean not with Startup) ?

Thank you for your kindly support...

1 Answer 1

0

You cannot launch a graphical application through upstart, or any other init system, because at the time that upstart is executing, X (the graphical environment) is not yet started.

In Windows, the graphical environment is an integral part of the operating system; even Windows Server Core is graphical at heart (even though it only presents you with a command prompt window). Linux, on the other hand, takes the opposite approach: the system itself is text-based only, and the graphical environment is provided by a userspace application running on top of that.

So unfortunately, there is no easy way to do what you appear to be after.

That said, you might be able to work around the limitation. Many desktop environments provide the ability to define a list of applications that will be launched when you log in (or more accurately, start the desktop environment). You could write a script that launches the application in a tight loop and exits when the application exits with a zero exit status (which is normal for "successful exit without a problem"). Whether this will work for you or not depends on your definition of "unexpected exit".

A naiive implementation might be something along the lines of:

#!/bin/bash
while true
do
    my-app || exit
    Xdialog --timeout 15 --yesno "Program exited. Restart it?" 2 50
    test "$?" = "0" -o "$?" = "255" || exit
done

The || means "execute the following if the exit status from the previous was non-zero" (it is read "or"). See the Xdialog documentation for how that one works; basically, it prompts you whether you want to keep running the program, and if you answer Yes or don't answer anything at all within 15 seconds, it relaunches the program by running the loop once more. Make the script file executable and point your desktop environment at launching it on login, and my-app will run until you explicitly close it and tell the system to not restart it.

1
  • I am using CentOs, I tried your suggestion, after starting this script, when I kill process of application(not restart script!). It gives core dump error and not there is no dialog for starting it again..
    – goGud
    Aug 6, 2014 at 8:49

You must log in to answer this question.

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