Running a Python script at reboot on Linux
Make a ‘.sh’ file:
- If you’re using SSH to connect your Raspberry Pi (i.e., in headless mode), type in the following command in the terminal window:
ssh pi@10.0.1.68 #where 10.0.1.68 is IP address of the SD card I am using.
- If you’re connected to a monitor, skip the above and continue with the next commands:
cd/home/pi/project #change your directory to the one in which the python scripts are stored.
nano atboot.sh #launches the text editor where you can type in your shell script(‘.sh’ file).
- Once the text editor opens, type in the commands to execute the python script:
#!/bin/bash # selects the shell in which you want to run
cd /home/pi/project #changes the directory to your work directory
date >> logs #inserts the date in logs file showing that this file has run
sudo python capture_and_save.py >>logs #redirects the errors and outputs of the python script to logs file
- Press CTRL+X to save the file.
*SH files- Developer files known as scripts that the Bash application programs and use. SH files can be executed if text commands are typed within the shell’s command-line interface.SH files are the scripts programmed using this application and they contain the commands to execute the program.
** The shell is responsible for interpreting your commands. Scripts (shell scripts) need shells to interpret them. When you run a shell script, your system needs to start up a shell process to execute your script. Bash is the most common shell used as a default shell for users of Linux systems.
Making the Python script executable:
Now one of the most basic tasks in Linux is setting file permissions.
There are three types of permissions you can apply:
- read — gives the group permission to read the file (indicated with r)
- write — gives the group permission to edit the file (indicated with w)
- execute — gives the group permission to execute (run) the file (indicated with x)
There are four groups you can give permissions to:
- user — the actual owner of the file
- group — users in the file’s group
- others — other users not in the file’s group
- all — all users
The order is quite important for both permissions and for groups. The order is always:
User Group Others — for groups
Read Write Execute — for permissions
Each permission can also be represented by a number. The numbers are:
Read — 4
Write — 2
Execute — 1
Here, we want to give all permissions to user so we add up the numbers for each permission and allot to user (i.e., 4+2+1=7) and just read and execute permissions (i.e., 4+1=5) to group and others. Hence, we enter the following command in the terminal window:
chmod 755 atboot.sh
Now, type in sh atboot.sh to run your python code.
Add to Crontab:
Open crontab by typing in:
sudo crontab –e
In the crontab window, type in:
@reboot sh /home/pi/project/atboot.sh >/home/pi/project/logs/cronlog 2>&1
#This command adds atboot.sh to the scripts that are to be run on reboot.
Reboot:
Reboot using sudo reboot
Wait for startup and check if the code works
If it doesn’t, check if the logs directory has been created in the assigned directory. If created, check for the file ‘cronlog’ and check the errors it is showing.