Introduction: Automate a Python Script to Run on Boot

About: I make things, sometimes they break

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.

Step 1: 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

Step 2: Make a Directory

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

Step 3: Create the Python Script

Create your Python 3 script named blinky that will blink the LED once the Pi Boots Up with the following command:

  • sudo nano

Here's the code :

from gpiozero import LED
from time import sleep

led =LED(4)

while True:




Control + X then press Y to save the file

Step 4: Wire the LED to Your Raspberry Pi

Wire the LED as shown on the Fritzing diagram above

Step 5: Create the Launching Script

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 .


  • sudo nano

Control + X then press the y key to save it

Step 6: 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 script executable,readable etc with the command:

  • sudo chmod 755

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

Control + C should stop the script from running

Step 7: Make a Debugging Directory

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

Step 8: Configure the Scripts to Run on Boot With Cron

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!


  • sudo crontab -e

then paste in the following at the bottom of the file

  • @reboot sh /home/pi/pythonTest/ >home/pi/logs/cronlog 2>&1

Control + X to save it

Step 9: 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