Automatic start of a GUI program

Hello,

I’ve browsed through multiple threads on this topic, but I’m still quite confused (and many of them are unsolved or updated only with further problems). I’m hoping someone can help me out on what seemed at first like a simple issue…

I am using JetPack4.2.1, flashed from Ubuntu 16.04… Let me know if additional info is needed please.

I have a Python program that takes information from a camera connected by USB and runs a GUI. I would like that program to open automatically, without input from the user, after the board powers on.

Adding regular programs to the Start Up Applications menu (Chromium or Solitaire, for example) seems to work fine, simply by clicking Add and typing in the command for the program. I’ve tried adding mine that way, and I’ve also tried adding it with “sleep 50” beforehand thinking maybe it was booting too soon, but no luck.

What can I do to make the GUI open automatically?

These are the threads I tried looking at:
https://devtalk.nvidia.com/default/topic/1021316/jetson-tk1/jetson-tk1-how-to-add-startup-application-i-want-to-execute-it-right-after-booting-up-/post/5198436/#5198436
https://devtalk.nvidia.com/default/topic/1029769/jetson-tx2/how-to-auto-run-shell-script-made-by-me-when-tx2-system-is-booted-/post/5238280/#5238280
https://devtalk.nvidia.com/default/topic/1042935/running-a-user-program-on-boot/
And this one seemed the most promising:
https://stackoverflow.com/questions/56412679/how-to-launch-python-program-at-startup-on-ubuntu-18-04-jetson-tx2
but not quite sure I follow.

From these, I tried writing the following script:

#! /bin/sh
sleep 20
python3 <path to program>
@reboot sleep 20 && python3 <path to program>

and then inserting it into the folder /etc/init.d/ but nothing happened.

Hoping someone can point out my mistake or give me some better advice :(

Edit to add -
I also tried adding the line that runs the script to the file in /etc/profile.d as suggested here:
https://devtalk.nvidia.com/default/topic/1056117/jetson-tx2/how-to-automatically-run-jetson_clocks-sh-on-tx2-at-startup-/
but didn’t work. Another answer there suggest adding to the folder /etc/rc.local, but that doesn’t seem to exist on my board…

You might try this:
https://websiteforstudents.com/set-apps-to-auto-startup-at-logon-on-ubuntu-17-10-18-04/

Some notes which may help:

  • GUI applications need to be run from the same user as the GUI is run under (there are ways around this, but significant effort would be required).
  • If the application cannot run manually in the GUI, then it won't run automatically either.
  • Running CUDA apps in a GUI tends to require the user be in group "video". If the app runs under that user, then you can assume the user is already a member of "video".
  • Any app run directly inside of a GUI will inherit the "DISPLAY" environment variable. To see which "DISPLAY" you are using, run this while in the GUI:
    echo $DISPLAY
    
  • Any script which is not run from in the GUI will require "DISPLAY" be set to the same as the "DISPLAY" from the GUI. If the GUI is not already running with that DISPLAY, then it will be impossible to open the app.
  • The file "/etc/rc.local" can be created if it doesn't already exist. It should be owned by root and have root execute permissions.
  • The file "rc.local" is not what you want if the command runs as someone other than root. You could use an "sudo" statement in the run, but keep in mind that if you relate to a GUI, then the GUI has to already be running and the DISPLAY must be correct...none of which exists at the time in boot where rc.local is run.

Hi linuxdev, thanks for the response!
The instructions in the link are similar to the first thing I tried (mentioned in my post, adding to Start Up Applications). For some reason, this works for other programs (like Firefox as mentioned in the tutorial), but not my Python program.

Re your notes:
What do you mean by “if it cannot run manually in the GUI”? Not sure I understood correctly what you mean, but if I type in the command in terminal after the computer has started up, the program works. If I copy that same command to Start Up Applications, it does not start up automatically.

echo $DISPLAY gives me :0, but I’m not entirely sure what to do with this. Should I have added this somewhere in my shell script?

Sounds like rc.local is indeed not the place for my script. Is there a different folder that runs after the desktop is set up that I should try instead? Or is the /profile.d folder the last thing that runs automatically?

That was what I meant. Since manual start works, then it is probably a missing environment variable needed for automatic startup.

rc.local is rarely the place to start a CUDA or GUI program. This runs prior to any GUI login.

What you might try is launching your program from a script. Let’s say the script runs program “hello”, you could do something like this to set environment within the auto start area (not rc.local):

#/bin/bash

DISPLAY=:0 /where/ever/it/is/hello

When an environment variable is provided on the command line like this it will be inherited by the program at the end of the command line. Alternately:

#/bin/bash

export DISPLAY=:0
/where/ever/it/is/hello

Hello,
I am having the same problem.
Python GUI is running when I manually open it from Terminal
But it is not running automatically.
I tried it by using startup application, crontab and even tried to create service but it is not working.

You should probably start a new topic. However, for a new topic, post the following:

  • When it works, then what do you see from this in the terminal it works from? “echo $DISPLAY”.
  • How is the program being started, e.g., from the user’s login triggering it, versus from the system without a user login.

Do note that the program might rely on a “DISPLAY” context, and if it does, then the GUI must be running (either a real GUI or a virtual one).

1 Like

Thank you for your response. I solved the problem. 😊