Auto-Run Python Program on Raspberry Pi Startup

Auto-Run Python Program on Raspberry Pi Startup

0 comments

In this tutorial, we will be covering some ways to run a python script on every boot up of your Raspberry Pi. There are many methods from which you can choose according to your script and requirements. This tutorial will make you understand how every Raspberry Pi login automatically the script executes.

 We will be covering 4 techniques to autorun a Python Script:

  1.  rc.local
  2. Crontab
  3. Autostart
  4. systemd

WRITE A PYTHON SCRIPT

Start with writing a Python script. You can write any program of your choice, here we are writing a python pattern for PiCube. 

PiCube is a 4x4x4 LED Cube for the Raspberry Pi. It can be used by beginners and professionals to strengthen their logic by typing complex code to draw out various patterns among its various uses. 

 This program will showcase a pattern every time the program executes. The name of the python program is Pattern1.py but you can write any other program like simply a LED blinking program or printing some sentences. The path of the program is /home/pi/PiCube/Pattern1.py.

METHOD 1: rc.local

On your Raspberry Pi terminal write the following command or you can also use your favorite editor to edit the file /etc/rc.local. Also, edit the permissions. This is useful if we want to plug our Raspberry Pi into power headless and have it run a program without configuration or a manual start.

Open rc.local file as super user-

sudo nano /etc/rc.local

After this, you will enter the nano file editor and here we have to add a command to execute our python program. Add the complete file execution line before ‘exit 0’ line at the end. After the editing saves the file and exit. For exit in nano type Ctrl x and for saving the file type Y.

Use ‘&’ at the end of the line if your program contains an infinite loop.

sudo python /home/pi/PiCube/Pattern1.py &

We add “&” at the end of the command as we need to end the command if the programs run continuously in an infinite loop we want to exit it. The ampersand will push the program to run on a separate process.

Now check whether the program will execute at the boot-up or not. Reboot your Raspberry Pi by adding this command: 

sudo reboot

How to kill Program

This python program will now execute on every boot-up and start-ups. Many times you will not be in need to run this program so to stop it we have “kill” the program. To kill down the process we have to do :

ps aux | grep /home/pi/PiCube/Pattern1.py

This command will give you all the details about the process like its process ID, location etc. the command will a line starts with root followed by the process ID of the file. We need this PID to kill the program. This command will be needed to kill the process: 

sudo kill PID

Note: rc.local also have drawbacks on Buster, Jessie and Stretch. Not all services are available on rc.local therefore not all the programs run properly.

Method 2: Crontab

Cron is an easy and important method for auto-run a script. The major advantage of Crontab is you can schedule tasks at any specific date or time and it will run the program without any delay. Many real-world applications uses Crontab for automation purpose.  

To make the program auto-run we have first edit the cron file. Every Raspberry Pi have its Crontab and to edit it we have to add “sudo” and “crontab -e”.

sudo crontab -e

A nano file will be open, scroll down and add the command to the end of the file. 

@reboot python /home/pi/PiCube/Pattern1.py

This command tells the Pi that on every reboot it has to execute the program which is there in the location given in the command. And the “&” again in the end of the command means that the command will run in the background and it won’t stop the system booting up as before.

Save the nano file and exit it by the Ctrl x and Y. Reboot the Raspberry Pi by:

sudo reboot

To stop the program again you have to the PID of the process and kill the process. When you don’t want to execute the program anymore just remove the command “@reboot  python /home/pi/PiCube/Pattern1/py” and the program will no longer run on boot ups.

Method 3: Autostart

Create the Desktop Entry

There is no need to change the user’s permission or any root-level access and in .desktop files. Also, we will not use “sudo”, this can cause a change in the permissions of the file and also the execution of the file by autostart.

Create a an autostart directory(if the autostart directory is not there) using the terminal and edit the PiCube.desktop file which has the PiCube Pattern python program. Follow these commands:

mkdir /home/pi/.config/autostart

nano /home/pi/.config/autostart/PiCube.desktop    

PiCube.desktop file will be open and copy the following lines in it. In the file add the name of the entry name the path of the python program. 

[Desktop Entry]

Type= Application

Name= PiCube

Exec= /usr/bin/python3  /home/pi/PiCube/Pattern1.py

Save and exit the nano file by Ctrl x and Y. For reboot the Raspberry Pi

sudo reboot 

Method 4: Systemd

The latest Raspbian have some of its boot sequences will lead some problems in running your python script using Cron or rc.local. “Systemd” is recommended to overcome such issues.  

Create a configuration file and edit it. This file will tell systemd which program needs to be executed :

sudo nano /lib/systemd/system/myscript.service

Add the following lines in the file:

[Unit]

Description=PiCube Pattern

After=multi-user.target

[Service]

Type=idle

ExecStart=/usr/bin/python3 /home/pi/PiCube/Pattern1.py

[Install]

WantedBy=multi-user.target

Save and exit the nano file using Ctrl+x,Y and ENTER.

Change the permissions on the configuration file to 644:

sudo chmod 644 /lib/systemd/system/myscript.service

Now all the tell the systemd to start the process on boot up :

sudo systemctl daemon-reload

sudo systemctl enable myscript.service

Now reboot your Pi and the process should run:

sudo reboot

Conclusion- 

Now that you are aware of the 4 methods to run your scripts when Raspberry Pi starts, you can try using all the methods described. These methods are very useful if you want to automate your tasks.

 

Hope this article was helpful to you.

Check out our new products Below: 

Micro:bit Go : Micro bit Starter kit 

Raspberry Pi Cluster HAT

 


Remote Access of Raspberry Pi Terminal via PuTTY - Windows and Linux

Raspberry Pi Dropbox Integration

Leave a comment

Please note, comments need to be approved before they are published.