Linux: Start an application automatically at boot

Josh Sakov
4 min readJul 6, 2020

There are different methods for starting an application automatically every time a Linux system boots up. We will survey a few methods that work on Ubuntu and on Raspberry PI.

Use Cases

Different methods work best for different use cases. To choose the best method, consider how you need your application to run:

  • Under a graphical user interface or not?
  • As a user or as a system process (daemon)?
  • Does it require automatic restarting if it crashes?
  • On a headless server (no monitor or keyboard) or on a desktop workstation?
  • What operating system (Raspbian, Ubuntu, other)?

Ubuntu with a graphical user interface, User Specific

Tested on Ubuntu 16.04 and Ubuntu 20.04

This method is user-specific. The application will be launched when the specific user logs in.

Instructions:

Log in as the target user. Open a terminal window and type:

gnome-session-properties

Alternatively, open Startup Applications via the Activities overview
A graphical user interface will show up. Click add. The rest is self-explanatory.
This will create a file, called a desktop item

~/.config/autostart/<name>.desktop

where <name> is the name you gave the application in the step above.

Raspberry PI With a Graphical User Interface, User Specific

Edit the file:

/home/pi/.config/lxsession/LXDE-pi/autostart
Or on older Raspbian versions:
/home/pi/.config/lxsession/LXDE/autostart

In that file, add a line to start each of your scripts:
Examples:

@/path/to/myapp
@/usr/bin/python /home/pi/example.py
@/usr/local/bin/electron /home/pi/electron_example.py

Notes:
1. Don’t forget the @
2. Your commands should come before the @xscreensaver line.

Update for recent Raspbian distribution
The stretch image removed ~/.config/lxsession and it's content.

Do:

mkdir -p ~/.config/lxsession
cp -R /etc/xdg/lxsession/LXDE-pi ~/.config/lxsession

Raspberry PI With a Graphical User Interface, All Users

The instructions are the same as above but the locations of the files are:

/etc/xdg/lxsession/LXDE-pi/autostart
Or on older Raspbian versions:
/etc/xdg/lxsession/LXDE/autostart

If the global file and the local file are both present, only the local file is executed.

An application that Requires no Graphical User Interface: Any Linux or Raspberry Pi Distribution, Single User

Method 1: Add a line to ~/.bashrc or
Method 2: Add a line to cron for each program:

Examples:

@reboot /path/to/job | at now + 2 minutes
@reboot sleep 60 && /path/to/job
@reboot /path/to/job

System Process (daemon): Any Modern Linux and Raspberry Pi Distribution,

This method is for the case when you want to run your program as a system process, whether users are logged in or not. This also works with a headless server.

Historically there were 3 frameworks in the Linux world for controlling system processes. The historical SysV, the intermediate Upstart, and the new systemd. Today systemd is used almost everywhere, including RHEL7 or later, Ubuntu 16.04 or later, and recent Raspberry PI distributions. Below are instructions for systemd.

Create the following file and save it as

/lib/systemd/system/<app-name>.service

Replace <app-name> with the name you want to give your service.

File content:

[Unit]
Description=A description of my application
After=multi-user.target

[Service]
Type=simple
ExecStart=/path/to/my/application
Restart=on-abort

[Install]
WantedBy=multi-user.target

Change the Description and the ExecStart entries as needed.

Then enter the following commands at the shell:

sudo chmod 644 /lib/systemd/system/<app-name>.service
sudo systemctl enable <app-name>.service
sudo systemctl daemon-reload
sudo systemctl start <app-name>.service

This approach is very configurable. Adjusting the configuration allows control of the conditions when the application is launched, whether it restarts after it crashes and the order of execution relative to other processes.

Quick and Easy System Service on Linux and Raspberry Pi Distribution,

This use case is similar to the previous one but does not require as many steps. The downside is that there is no automatic restart after a crash and no precise control of the order of execution. Also, it is not supported by some modern distributions. Your program will start after all other programs have been initialized.

Compatibility:

  • Ubuntu 16.04 — yes
  • Ubuntu 18.04 and later — No
  • Raspbian 10 — yes
  • Raspberry PI OS (buster)— yes

Edit the file /etc/rc.local

Add the following at the bottom:

/path/to/my/script

Node.js application

To start a node.js application automatically, including restart upon a crash and also whenever the source code changes you can use the node forever library.

sudo npm install forever -g
sudo npm install forever-service -g
sudo forever-service install <my-application> -- script /path/to/my/script.js
sudo service <my-application> start

The following tools allow you to control the program:

sudo service <my-application> start
sudo service <my-application> stop
sudo service <my-application> status
sudo service <my-application> restart

References

1. https://gist.github.com/emxsys/a507f3cad928e66f6410e7ac28e2990f
2. https://wiki.archlinux.org/index.php/systemd
3. https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
4. https://coreos.com/os/docs/latest/getting-started-with-systemd.html
5. https://www.raspberrypi-spy.co.uk/2014/05/how-to-autostart-apps-in-rasbian-lxde-desktop/
6. https://blog.startingelectronics.com/auto-start-a-desktop-application-on-the-rapberry-pi/
7. https://www.raspberrypi.org/documentation/linux/usage/systemd.md
8. https://askubuntu.com/questions/48321/how-do-i-start-applications-automatically-on-login

--

--