Raspberry Pi

How to Launch Any Program at the Startup of Raspberry Pi

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:

$ nano welcome.py

And type the following text in the file:

#! /usr/bin/env python

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:

$ sudo apt install espeak -y

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:

$ sudo nano /lib/systemd/system/welcome.service

Now add the following script to the file of welcome.service:

[Unit]

Description=My welcome Service

After=multi-user.target

[Service]

Type=idle

ExecStart=/usr/bin/python /home/pi/welcome.py

[Install]

WantedBy=multi-user.target

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:

$ sudo chmod 644 /lib/systemd/system/welcome.service

After changing the file permissions, we will reload the daemon and enable the newly created “welcome” service with the command:

$ sudo systemctl daemon-reload && sudo systemctl enable welcome.service

Reboot the system to confirm the changes:

$ reboot

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.

Conclusion

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.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.