Running a script or program on the Raspberry Pi is as simple as finding it on the system and executing it. But what if you want to automate this process, perhaps because your project requires it or because you want to streamline your workflow?
One answer to this is learning how to use systemd.
What Is Systemd?
systemd is a suite of system components for service configuration and behavior on modern Linux systems. It’s identified with a PID (process identifier) of 1 since it’s the first process to boot up at system startup. At large, systemd’s purpose is to help you with managing and executing programs at system startup. And that’s what you’ll be leveraging to launch your program at startup on the Pi.
As mentioned initially, there are several methods to run programs at startup on Linux systems, such as rc.local, cron, and autostart. However, systemd happens to a better solution of the lot if you want to launch GUI (graphical user interface) programs, automate applications to run after certain system processes begin, or run programs over and over again until your scheduling works.
How to Use systemd to Launch Programs at Startup
systemd is a slightly more complicated method of launching programs at startup on Linux systems. However, the scheduling flexibility it offers over other methods — the ones we’ve mentioned above — totally outweighs its complexity in every aspect.
The systemd method uses unit files, which are kind of like .ini files that hold encoding information about devices, services, sockets, start-up target, and other essential system components.
Unit files are of various types. So, based on what kind of resource you want to declare, you need to choose a unit file type accordingly. For the purpose of this guide, though, we’ll stick to .service unit files, which allow you to manage the startup behavior of programs and scripts on the system.
Below is a breakdown of the steps involved in launching a script/program at startup using systemd.
Creating a Service
To set your program to run at startup, create a service unit file that will tell systemd which program to run and when. Here’s how to do it.
In the CLI window, type sudo nano /lib/systemd/system/display. service. You can use any name for your service as long as it ends with the . service extension. In the service file, paste the following lines of code: [Unit]Description=PiCounterAfter=network. target[Service]ExecStart=/usr/bin/python3 /home/pi/PiCounter/display. pyRestart=alwaysUser=pi[Install]WantedBy=multi-user. target
All your common configuration options fall under the Unit and Install sections, while the service-specific configuration options go under the Service section.
A few of the above directive-value pairs you might need to modify include:
i. After: It determines when to start the service. Setting it to network.target ensures that the listed value — network.target in this case — starts before beginning the current unit. However, it does not direct a dependency relationship for the service to trigger; that’s done through other directives such as Wants, Requires, and WantedBy.
Of course, you can change this value to suit your project requirements. For instance, if you want your program to execute after your Raspberry Pi connects to a network, you can use the network-online.target value. You can refer to Fedora’s documentation on systemd to learn more about these values.
ii. ExecStart: It holds the absolute path and the program/script name that you want to execute at startup. So depending on what you want to execute, you need to add values for both these directives.
iii. User: It identifies the name of your Raspberry Pi. If you’ve changed your Pi’s name, you need to put that value for user.
iv. WantedBy: It is the most common way of specifying the trigger for a unit. By setting it to multi-user.target, you can establish a dependency relationship that will run the program when the system control is handed to the multi-user shell.
Moreover, this also runs your program before Pi’s X-Windows system starts, which means your program runs even before you log in to the system. In case you want a script to run before you log in to your Raspberry Pi, this enables you to do so.
The above lines of code work for non-GUI programs. However, if you want to run a GUI program using systemd, you should add the following code to your service file:
In the above directive-value pair, all you need to change is the value for the ExecStart directive. Replace the one in the codeblock with the program you want to run at startup. After you’ve populated the service file, hit CTRL + O to save it.
Test the Service
With the service ready, manually verify if it works. For this, type the following command in the terminal: sudo systemctl start display.service. The service should execute your instructions successfully.
Once it does, terminate it with sudo systemctl stop display.service.
Enable the Service
If you managed to run your program successfully using the service, it’s time to enable it so that it executes your program automatically every time you boot up your Raspberry Pi.
To do this, type sudo systemctl enable display.service in the terminal window. And once that’s done, enter sudo reboot to restart your Pi.
Once your Raspberry Pi boots up, systemd should run your program as per your provided instructions.
Automating Program Launch at Startup on Raspberry Pi
Like most Linux systems, Raspberry Pi OS also lets you accomplish operations in several ways, and it remains true to scheduling program launches as well. So, if you want to launch GUI programs/scripts at startup and want better control over your scheduling, systemd is clearly a better method.
That said, though, if you want a simpler solution or your project requirements demand specific scheduling, you can check our guide to Running a Raspberry Pi Program or Script at Startup to learn more.