We want to display some message on the startup of the Raspberry Pi but are we thinking about whether it is possible or not? Then yes it is possible and in this article, we will learn the method of displaying the message or we can launch any other program on the startup of the Raspberry Pi. For this purpose, we will make some changes to the systemd file, and then after these changes, it launches our customized program on the startup of the Raspberry Pi.
What is a systemd file in the Raspberry Pi
The systemd file in the Raspberry Pi controls the execution of different programs at the boot time and is also responsible for the activation of the daemons. The systemd system also manages the operation of different services; for example, it can be used to start, stop, and check the status of the Apache web server service. As we said that the systemd system controls the operation of different programs at boot time, we can use it to launch a specific program on the startup of Raspberry Pi.
We will make a Python script and play a sound of “Welcome to the LinuxHint” on the startup of the Raspberry Pi, for that, we will create a file with the name “welcome.py” using the nano text editor:
And type the following text in the file:
from subprocess import call
call([‘espeak “Welcome to the LinuxHint” 2>/dev/null’], shell=True)
Save the file and exit the nano editor, but make sure the “espeak” is installed on the Raspberry Pi, if it is not installed, then use the mentioned command to install it:
How to launch any program on startup of Raspberry Pi using the systemd file
To launch a program at the boot time of the Raspberry, we will define a new service and we will create a service with the name of “welcome” at the /lib/systemd/system/ using the nano editor:
Now add the following script to the file of welcome.service:
Description=My welcome Service
In the above script, we describe the service as a My welcome Service, which will be launched after the multi-user.target. Then in the type, we define its behavior as “idle” so it will be launched when other major operations will be loaded and then give the path and name of the file which is to be loaded. After this save the file by using the shortcut of CTRL+S and exit the text editor with CTRL+X.
Now we will change the permission of the newly created ”welcome” service file using the chmod command:
After changing the file permissions, we will reload the daemon and enable the newly created “welcome” service with the command:
Reboot the system to confirm the changes:
Using this method we can launch any program on the start-up of the operating system, all you need to do is make a few changes in the Python code.
We can launch any program at the boot time of the Raspberry Pi operating system by using different ways like rc.local, systemd, and .bashrc. The systemd is the most used and convenient method to launch any program at the startup of Raspberry Pi. In this write-up, we used the systemd method to launch a Python script on the startup of Raspberry Pi.