Automating Raspberry Pi: How to Autostart Programs

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.

In this tutorial you will learn:

  • How to auto start a program with rc.local file
  • How to auto start a program with crontab
  • How to auto start a program with systemd
  • How to auto start a program with GNOME Tweaks
Automating Raspberry Pi: How to Autostart Programs
Automating Raspberry Pi: How to Autostart Programs
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.

Editing the rc.local file to configure a script to automatically start
Editing the rc.local file to configure a script to automatically start

When done, exit the file and save changes.

Using cron

Another simple way to make a program start automatically on your Raspberry Pi is by setting it up in cron.

  1. Type the following command to edit crontab:
    $ crontab -e
    
  2. 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

  3. 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:
    @reboot /home/linuxconfig/myscript.sh
    
    Editing crontab to auto start a program on Raspberry Pi
    Editing crontab to auto start a program on Raspberry Pi

    Save your changes and exit the file when done.

DID YOU KNOW?
In case you want to auto start your program or script every minute, week, month, etc, you can configure crontab to do more than just execute the program every reboot. See our other tutorial on Linux crontab tutorial with Examples to see how to schedule execution of a program at other intervals.

Using systemd

We can also use the Raspberry Pi’s systemd to create a service file that gets executed upon boot.

  1. First, create a Systemd service file as in an example below. We will store this file as /etc/systemd/system/disk-space-check.service.
    [Unit]
    After=network.target
    
    [Service]
    ExecStart=/usr/local/bin/disk-space-check.sh
    
    [Install]
    WantedBy=default.target

    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
    WantedBy: Into what boot target the systemd unit should be installed



    NOTE
    For more information on how to create Systemd service unit execute the man systemd.unit command.
  2. 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
  3. Set appropriate permissions for both, the Systemd service unit and script:
    $ sudo chmod 744 /usr/local/bin/disk-space-check.sh
    $ sudo chmod 664 /etc/systemd/system/disk-space-check.service
    
  4. Next, enable the service unit:
    $ sudo systemctl daemon-reload
    $ sudo systemctl enable disk-space-check.service
    
  5. 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.

  1. Let’s start by opening a command line terminal and using our apt package 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
    
  2. 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
  3. Once installation is completed, open up GNOME Tweaks by executing this command:
    $ gnome-tweaks
    
  4. In GNOME Tweaks, go to ‘Startup Applications’ on the left side.
    Click the Startup Applications menu in GNOME Tweaks
    Click the Startup Applications menu in GNOME Tweaks
  5. 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
  6. Select the program that you want the Raspberry Pi to auto start. In this example, we will select VLC.
    Select the program that you want your Raspberry Pi to auto start at boot
    Select the program that you want your Raspberry Pi to auto start at boot
  7. Once done, you will see your selections and can close this window for all the changes to take effect.

    We now see which programs are set to auto start
    We now see which programs are set to auto start



Closing Thoughts

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.



Comments and Discussions
Linux Forum