Automate a Python Script to Run on Boot
by NemesisC in Circuits > Raspberry Pi
4658 Views, 40 Favorites, 0 Comments
Automate a Python Script to Run on Boot
![raspi.png](/proxy/?url=https://content.instructables.com/FCM/27C2/IUYD6LCF/FCM27C2IUYD6LCF.png&filename=raspi.png)
If you’re constantly moving your Raspberry Pi projects from place to place this tutorial will show you how to automate a Python script so you can take your pi on the go!
I found this especially helpful when working with the Pi Zero because you won’t need to crank out the adapters, ethernet adapter etc to be able to run your Python scripts when out.
Updating and Installing GPIO Zero 3
I'm assuming you can access the terminal of your Raspberry Pi (ssh,VNC, on a monitor … etc)
Run these commands in order
Let’s update the Pi and install the newest version of GPIO Zero ver 3 for Python 3 by running the following commands:
- sudo apt-get update
- sudo apt-get install python3-gpiozero
Make a Directory
![Screen Shot 2016-09-21 at 9.24.52 AM.png](/proxy/?url=https://content.instructables.com/FWC/K16B/IUYD6CIT/FWCK16BIUYD6CIT.png&filename=Screen Shot 2016-09-21 at 9.24.52 AM.png)
![Screen Shot 2016-09-21 at 9.26.27 AM.png](/proxy/?url=https://content.instructables.com/FR6/IUSA/IUYD6CJ1/FR6IUSAIUYD6CJ1.png&filename=Screen Shot 2016-09-21 at 9.26.27 AM.png)
Make a directory(folder) to store your python scripts . I will create one named pythonScripts:
- sudo mkdir pythonTest
then navigate to the directory using this command:
- cd pythonTest
Create the Python Script
![Screen Shot 2016-09-23 at 2.38.37 PM.png](/proxy/?url=https://content.instructables.com/F1S/ZSN3/IUYD6CJ4/F1SZSN3IUYD6CJ4.png&filename=Screen Shot 2016-09-23 at 2.38.37 PM.png)
Create your Python 3 script named blinky that will blink the LED once the Pi Boots Up with the following command:
- sudo nano blinky.py
Here's the code :
from gpiozero import LED
from time import sleep
led =LED(4)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
Control + X then press Y to save the file
Wire the LED to Your Raspberry Pi
![piZeroWiring.png](/proxy/?url=https://content.instructables.com/FCG/46YT/IUYD6CKI/FCG46YTIUYD6CKI.png&filename=piZeroWiring.png)
Wire the LED as shown on the Fritzing diagram above
Create the Launching Script
![Screen Shot 2016-09-23 at 2.52.05 PM.png](/proxy/?url=https://content.instructables.com/FXO/MAT1/IUYD6CJK/FXOMAT1IUYD6CJK.png&filename=Screen Shot 2016-09-23 at 2.52.05 PM.png)
Next we need to create the script that will tell the Raspberry Pi where our blinky Python script is located to run it o boot .
Type:
- sudo nano launcher.sh
Control + X then press the y key to save it
Make the Launcher Script Executable
Since in Linux there is a set of rules for each file which defines who can access that file, and how they can access it.We need to make the launcher.sh script executable,readable etc with the command:
- sudo chmod 755 launcher.sh
chmod stands for "change mode" used to define the way a file can be accessed.
755 means the owner may read, write, and execute the file more information about chmod 755 found here
Now test it by typing: this should turn on your LED!
- sh launcher.sh
Control + C should stop the script from running
Make a Debugging Directory
![Screen Shot 2016-09-23 at 3.12.26 PM.png](/proxy/?url=https://content.instructables.com/FGE/L2LV/IUYD6CJL/FGEL2LVIUYD6CJL.png&filename=Screen Shot 2016-09-23 at 3.12.26 PM.png)
Create a directory called logs for storing any errors information if the script fails the run on boot .
Navigate back to the home directory by typing:
- cd
Next type:
- sudo mkdir logs
Configure the Scripts to Run on Boot With Cron
![Screen Shot 2016-09-23 at 3.21.26 PM.png](/proxy/?url=https://content.instructables.com/FTT/BEGB/IUYD6CJO/FTTBEGBIUYD6CJO.png&filename=Screen Shot 2016-09-23 at 3.21.26 PM.png)
Now let’s access the crontab-a background (daemon) process that lets you execute scripts at specific times- so we can run our script on boot. learn about Cron here!
Type:
- sudo crontab -e
then paste in the following at the bottom of the file
- @reboot sh /home/pi/pythonTest/launcher.sh >home/pi/logs/cronlog 2>&1
Control + X to save it
TEST IT ! Reboot Your Pi
Make sure your LED is correctly wired to pin 4 and Ground then
Reboot your pi
- sudo reboot
wait a minute or two for the Pi to boot up then if it worked you LED should blink!
If your script did not work you can check the debugging folders we made by typing
- cd logs
then read the cronlog that should have any errors that occurred on boot using the command cat
- cat cronlog