This article is to provide step by step instructions to run a Python code in the boot of Raspberry Pi automatically. Sometimes we may want to run a Python script when Raspberry Pi boots up, like Webserver applications need an auto start of the server so that remote access is possible. There are a number of different techniques to do this, one of those is crontab. A crontab is a job scheduler that allows the system to perform tasks at defined time or intervals. It is a very powerful tool and useful in lots of situations. You can use it to run commands in the boot or a python script in this case.
In this Tutorial the Raspberrypi board is booted with the Ubuntu OS and is connected to the Ethernet port of a Windows7 PC. The board is connected to the internet connection as explained in the article Connecting the Raspberrypi to the internet. The IP address of the Raspberrypi board has been obtained to remote login in the TUI using the PUTTY and is remotely accessed using VNC.
Make a launcher script:
The Python script which is to be run at boot is camera.py which is in a directory called camera in the home directory. We will use the Linux crontab to run a shell script, which always navigates to the proper directory and will launch our Python script.
Create a shell script by typing the command below
sudo nano launcher.sh
#!/bin/sh
cd /
cd home/pi/camera
sudo Python camera.py
cd /
Ctrl-X, Return to save.
Fig. 2: Making A Launcher Script For Navigating to Python Script
What this script will do is to navigate to the root directory, then to the camera directory, launch the Python script and then return to the root directory.
Make it executable:
To make the launcher script an executable, use this command
sudo chmod 755 launcher.sh
Now you can test the Shell script by entering the command below. This should run your Python code.
sh launcher.sh
Fig. 3: Commnad To Make Launcher Script Excutable
Add logs directory:
To Create a log file, we need to make a directory which will encloses all the Log file in which the errors will be entered.
sudo mkdir logs
Add to your crontab:
crontab is a background process that lets you execute scripts at specific times. It’s essential to Python and Raspberry Pi. The details are confusing, as is often the case with Linux.
Here’s the Linux reference and here are some more crontab examples.
To open crontab type the below command in terminal
sudo crontab -e
Now, enter the line:
@reboot sh /home/pi/camera/launcher.sh >/home/pi/camera/logs/cronlog 2>&1
Fig. 4: Adding Contab To Execute Python And Raspberry Pi Scripts
Reboot and see if it works by typing the reboot command
sudo reboot
check out the log file:
cd logs
nano cronlogAdd logs directory:
To Create a log file, we need to make a directory which will encloses all the Log file in which the errors will be entered.
sudo mkdir logs
Add to your crontab:
crontab is a background process that lets you execute scripts at specific times. It’s essential to Python and Raspberry Pi. The details are confusing, as is often the case with Linux.
Here’s the Linux reference and here are some more crontab examples.
To open crontab type the below command in terminal
sudo crontab -e
Now, enter the line:
@reboot sh /home/pi/camera/launcher.sh >/home/pi/camera/logs/cronlog 2>&1
Fig. 5: Add Logs Directory For Error In Scripts
This will show you any errors or logs that you might have.
Filed Under: Raspberry pi
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.