Getting programs to start up automatically on your Raspberry Pi will help you to automate the device for certain tasks. Most users are likely not going to use their Raspberry Pi as a daily driver, so they will need services and programs to start up automatically without further user intervention. Whenever we can automate such tasks, it makes less work for us. In this tutorial, we will go over the step by step instructions to enable programs to autostart via command line and GUI on a Raspberry Pi.
Software Requirements and Linux Command Line Conventions
Category
Requirements, Conventions or Software Version Used
System
Raspberry Pi
Software
rc.local, systemd, crontab, GNOME Tweaks
Other
Privileged access to your Linux system as root or via the sudo command.
Conventions
# โ requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ โ requires given linux commands to be executed as a regular non-privileged user
How to Auto Start Programs via Command Line
When using the command line, there are several different ways we can configure a program to auto start on the Raspberry Pi. We have split the various methods into subsections below. Feel free to use whichever one seems easiest for you or most appropriate for the situation at hand.
Using the rc.local file
This is a handy file that gets executed at the end of each multiuser runlevel. We can append lines to the file of commands that we want to run when the Raspberry Pi boots up, or point it to a script to execute. Access the file by opening it in a text editor like nano:
$ sudo nano /etc/rc.local
Add your commands or the path to your script that you want executed to the end of the file. Make sure to leave exit 0 at the very end of the file to make sure it continues working as intended.
Another simple way to make a program start automatically on your Raspberry Pi is by setting it up in cron.
Type the following command to edit crontab:
$ crontab -e
If this is your first time opening crontab, you will be asked which editor you want to use. The simplest one to use would be nano, so select this option if in doubt. ๐ Selecting a default text editor to use with crontab Selecting a default text editor to use with crontab
Use the following syntax to add your script or command that you want cron to execute at every reboot. We are using the @reboot syntax:
After: Instructs systemd on when the script should be run. In our case the script will run after network connection. Other example could be mysql.target etc. ExecStart: This field provides a full path to the actual script to be executed on startup. To learn more about this, you can create systemd service units for your own scripts. WantedBy: Into what boot target the systemd unit should be installed
Create a script to be executed on Raspberry Pi system startup. As specified in the above Step 1, the path and the name of the new script in our example will be /usr/local/bin/disk-space-check.sh.
The below is an example of such script:
#!/bin/bash
date > /root/disk_space_report.txt
du -sh /home/ >> /root/disk_space_report.txt
Set appropriate permissions for both, the Systemd service unit and script:
Now you are ready to reboot your Raspberry Pi. Once the system boots you should see the following file containing disk space usage within your /root directory:
$ sudo ls /root/
disk_space_report.txt
How to Auto Start Programs via GUI
The GUI method we will use in this section involves installation and configuration of GNOME Tweaks. You can install and use this program whether you are on the GNOME desktop environment or not (the default desktop environment for Raspberry Pi is LXDE), so you do not need to worry about switching desktop environments just to get auto starting program to work.
Letโs start by opening a command line terminal and using our aptpackage manager to install GNOME Tweaks (as mentioned, the GNOME desktop environment will not need to be running to use GNOME Tweaks!):
$ sudo apt update
$ sudo apt install gnome-tweaks
During installation, you may be prompted about which display manager to use by default. If you want to stick with the default LXDE desktop environment, then choose lightdm. If you want to switch over to GNOME, feel free to select gdm3.
๐ Choose your default display manager for Raspberry Pi Choose your default display manager for Raspberry Pi
Once installation is completed, open up GNOME Tweaks by executing this command:
Under the Startup Applications menu, click on the plus sign icon to add a new program that you want to auto start.
๐ Click on the plus sign to add a new program Click on the plus sign to add a new program
In this tutorial, we saw how to configure programs to auto start on a Raspberry Pi. This allows us to make programs, services, or scripts execute automatically upon system boot when we first load into the Raspberry Pi. Automating such tasks saves valuable time for the user or system administrator, especially if the Raspberry Pi is not being used as a daily system and needs to operate without much user intervention.