Setting up the Pi to run Python scripts
All right, your Pi is up and running, now you just need to configure it to run Python scripts.
The good news is: there’s not much to do if you are using Raspbian, as it has Python installed out of the box
One thing you can do is download the latest version, probably because Raspbian does not have the latest release. Head over to python.org, and upgrade if necessary.But other than this you are good to go. You can copy your existing .py files and start using them with the built in IDE or Terminal straight away. Speaking of which: if you are copying over files you used on Windows or OS X, remember that on Linux you’ll need a different shebang line. You probably know where to look this up, but here it is for reference:
- On Windows, the shebang line is
- On OS X, the shebang line is
#! /usr/bin/env python3.
- On Linux, the shebang line is
(shebang lines are the first lines in python script files that tells the system where to locate the interpreter)
Also, if you want to start scripts from the terminal there are a few extra steps to do:
- Place them .py files in your home folder
- Make them executable with the following command:
chmod +x script.py
Now you can start a script by typing in
./script.py to the terminal
(If you place them somewhere else than your home folder you will have to describe the whole path to start them)
Automating scripts on the Pi with CRON
Now to the interesting part. So far we set up our Pi and started to run python scripts on it. But logging in through RealVNC or SSH every time we want a script to run is not really convenient (with the exception if we want to trigger scripts via SSH coming from another device or service automatically, but this will be detailed in another post).
If we have a web-scraping script that looks for some info on the web every day and returns some information (like the food notifier explained in my previous post), then what we want is total automation. The script should start all by it self, do it’s job, and return the information.
Luckily we can do that with a built in tool in Linux called CRON.
How to setup CRON
- Log into your Pi with realVNC, and start Terminal.
- In the Terminal, enter
crontab -e. If you want a command to run with system permissions, use the
sudo crontab -e command to open the root account’s crontab file.
- You might be asked to select an editor. If this happens choose Nano (for me this was the default)
- You will be presented with the following screen:
Every line that starts with a
# is just a comment and will be not executed.
- Use the arrows or Page Down to scroll down to the bottom of the page
- Each line you add here will be executed.
The format for the parameters is the following:
Minutes Hours Day of month Month Weekday Command
If you want to skip a parameter, just add
* as input.
From the Raspberry Pi Official API:
Scheduling tasks with Cron
Cron is a tool for configuring scheduled tasks on Unix systems. It is used to schedule commands or scripts to run periodically and at fixed intervals. Tasks range from backing up the user’s home folders every day at midnight, to logging CPU information every hour.
crontab (cron table) is used to edit the list of scheduled tasks in operation, and is done on a per-user basis; each user (including
root) has their own
A graphical application for Cron is available by installing the
sudo apt-get install gnome-schedule
You can then launch the program Scheduled Tasks from the main menu.
crontab with the
-e flag to edit the cron table:
Select an editor
The first time you run
crontab you’ll be prompted to select an editor; if you are not sure which one to use, choose
nano by pressing
Add a scheduled task
The layout for a cron entry is made up of six components: minute, hour, day of month, month of year, day of week, and the command to be executed.
# m h dom mon dow command
# * * * * * command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
0 0 * * * /home/pi/backup.sh
This cron entry would run the
backup.sh script every day at midnight.
View scheduled tasks
View your currently saved scheduled tasks with:
Run a task on reboot
To run a command every time the Raspberry Pi starts up, write
@reboot instead of the time and date. For example:
@reboot python /home/pi/myscript.py
This will run your Python script every time the Raspberry Pi reboots. If you want your command to be run in the background while the Raspberry Pi continues starting up, add a space and
& at the end of the line, like this:
@reboot python /home/pi/myscript.py &