How to start GUI application with upstart?

7,609

Solution 1

The problem you are facing is that when upstart (or systemd, or the scripts in /etc/rc.d/) are run, there is normally no graphic service ("the X server") running.

Moreover, the availability of the graphic subsystem in Unix is strictly bond to the concept that a user has done a graphic login, and just this user has the right to use the graphic environment. It is customary NOT to start a graphic subsytem for root --- and the upstart scripts are run by root.

To automatically start a graphic application at the start of the system, my approach would be:

  1. create a user for this purpose. Set it up so that its session will autostart.
    enter image description here

  2. set up a startup application for this user with the program you want; choose "startup application" in the dash: enter image description here

  3. for restarting the application when it exits/crashes, you can simply embed it in a script:

         #!/bin/bash
         #
         while true; do 
              /full/path/to/start_myapp.sh    # NO background (&)!
              # if we land here it exited
              sleep 5
         done
    

If you use this script, it is really important that the command start_myapp.sh should not launch the application in background. Otherwise, more complex strategies are required to auto-restart...

Notice that you can use your normal user in parallel too; just choose "switch user" from the panel (adapt to your flavor of Ubuntu) and you will have another graphical login screen; you can switch back-an-forth using CTRL-ALT-F7 and CTRL-ALT-F8...

Solution 2

Create a file $HOME/.config/upstart/myGuiStart.conf

Content:

start on desktop-start
stop on desktop-end

respawn

exec firefox

or another example with a delay:

start on desktop-start
stop on desktop-end

respawn

script
    sleep 30
    firefox 
end script

description of respawn:

 respawn
         A service or task with this stanza will be automatically started
         if it should stop abnormally.  All reasons for a service stopping,
         except the stop(8) command itself, are considered abnormal.  Tasks
         may exit with a zero exit status to prevent being respawned.

More info:

http://ifdeflinux.blogspot.de/2013/04/upstart-user-sessions-in-ubuntu-raring.html

http://upstart.ubuntu.com/

Respawn bug? -> https://askubuntu.com/a/62461/265974

Share:
7,609

Related videos on Youtube

goGud
Author by

goGud

C++ and Qt developer

Updated on September 18, 2022

Comments

  • goGud
    goGud over 1 year

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

    • Tim
      Tim almost 10 years
      What is upstart? Do you just want to run it at login?
    • goGud
      goGud almost 10 years
      No I know how to use with StartUp application... I want to use upstart /etc/init/XXX.conf
    • Tim
      Tim almost 10 years
      To what? Start at a certain time? Couldn't you use cron?
    • goGud
      goGud almost 10 years
      I have an application written with c++/qt which has Graphical User Interface.. I will put this application in another pc which will run 7/24.. And if there is an unexpected close in application, I want to run itself. Without any operator interactioon..
    • Benoit
      Benoit almost 10 years
      @tim: upstart is new way of starting processes introduced some time ago in Ubuntu. It replace the traditional System V startup scripts (rc directories, /etc/init.d/, ...). The main advantage of upstart on the traditional startup mechanism (for me at least) is that he has a respawn mechanism built-in, allowing to automatically restart stopped services. And certainly many more other advantages (like strong dependencies mechanism and so on).
    • Tim
      Tim almost 10 years
      @Benoit Okay, I don't know anything about that, I wondered if I could suggest cron.
    • Jacob Vlijm
      Jacob Vlijm almost 10 years
      Would this be a suggestion: askubuntu.com/a/503822/72216
    • goGud
      goGud almost 10 years
      @Tim: Yes, cron is an option but it has a bit differences between cron. And I do not have any schedule.. So it is better to do it with upstart.. However, if I do not solve this problem with upstart, I can use cron.. :(
  • goGud
    goGud almost 10 years
    You suggest me to use startup, which I do not prefer. But if i can re-start it when application down. I can also use this.. Could you please help me with more detail.. Lets says that I have myApp.sh which starts another binary file related to my GUI. #!/bin/bash ./start_myapp.sh ##End of script what should I write in here ?
  • goGud
    goGud almost 10 years
    But your suggestion re-start application in every 5sec, am I right ?
  • Rmano
    Rmano almost 10 years
    @goGud: login, user options, startup scripts, X setup and similar can be wildly different even in different flavors of Ubuntu. Even more between different distributions; I think CentOs uses systemd for example and not upstart (but I can be wrong). So for the details better check in the right forums...
  • TuKsn
    TuKsn almost 10 years
    There is no problem to use upstart to start a gui -> ifdeflinux.blogspot.de/2013/04/…
  • Rmano
    Rmano almost 10 years
    @TuKsn really interesting --- the upstart user session is started after login? Or you can just start a GUI application (say, a guest session browser) without any login? Would be nice if you can add an answer here...