Tooling Tuesday - Auto Start a GUI application in Raspbian

Auto starting an app in Linux / Raspbian is an interesting activity...

For common scripts to run at boot we can use

  • rc.local
  • .bashrc
  • init.d
  • systemd
  • crontab

Source Dexter Industries blog post

These will all run our script before the Raspbian desktop has loaded and for general use that is cool!

But recently I wanted to load a script when the Raspbian desktop started, and this lead me to discover LXDE autostart.

Please Note

This was tested with the latest release of Raspbian on a fresh SD card. It should work with older Raspbian releases, but your mileage may vary.

So what is it?

LXDE is the window manager for Raspbian, it is what we use to interact with the operating system.

In order to load an application when the LXDE desktop loads we need to create a file that will auto start.

So how can I make one?

First we need to create a file that will contain the configuration to auto start our application. This file will be located in /etc/xdg/autostart and we shall call it My-Cool-App

$ sudo nano /etc/xdg/autostart/My-Cool-App.desktop

Inside the file we need to create the following structure

[Desktop Entry]
Type=Application
Name=NAME OF APP
Comment=WHAT DOES IT DO?
NoDisplay=false
Exec=COMMAND TO RUN
NotShowIn=GNOME;KDE;XFCE;

So we start with the Type, in this case it is an Application, the Name of the app is rather self explanatory, as is the Comment but the comment is what we see if we hove the mouse over the desktop icon. NoDisplay controls if the app can be seen or hidden in startup applications. The most important part of this file is Exec as this is the command / script to be executed.

So lets create an example for our app. It will open the Chromium browser when the desktop loads, and open it to this blog.

[Desktop Entry]
Type=Application
Name=My Cool App
Comment=Opens the best website ever!
NoDisplay=false
Exec=chromium-browser https://bigl.es
NotShowIn=GNOME;KDE;XFCE;

To save press CTRL + O, then press ENTER, then CTRL + X to exit.

Ok so now we need to reboot the Raspberry Pi and when it loads the desktop, it will open Chromium to this website!

So now what?

Well we can use this method to load anything when the desktop loads, for example lets say that we have a Python3 app that we would like to load.

[Desktop Entry]
Type=Application
Name=Python 3 GUI App
Comment=Opens my cool Python 3 app
NoDisplay=false
Exec=python3 /home/pi/my-python3-app.py
NotShowIn=GNOME;KDE;XFCE;

Happy Hacking!