Codementor Events

How to run and schedule Python scripts on Raspberry Pi

Published Sep 05, 2018Last updated Mar 04, 2019
How to run and schedule Python scripts on Raspberry Pi

Previously I wrote about how and why I built a simple web-scrapig script to notify us about our favourite food. I followed up with a post that detailed how to run and schedule Python scripts on iOS, and now I'd like to share how you can do the same with a Raspberry Pi.

Why the Pi?

While it's possible to play around and fire off simple Python scripts on iOS it has it's limitations.
First, you can't truly automate running them. You can trigger them by time or location, but you will have to unlock your phone and hit OK on each trigger.
Second, you can't take advantage of some of the more advanced modules. One example is Selenium, which can create virtual browser instances in the background, visit websites and take actions on them. This is not possible due to iOS's locked down architecture.
On the Pi (Linux) you have no limits. You can use any module and schedule scripts to run fully automatically, whenever you like.

Ok, but the Pi is running Linux. Why not just buy and old laptop running Linux and use that instead?

You can certainly do that, but I think the Pi is much more practical. It has the size of a bank card. If you put it in a case it's more like a PowerBank. If you're like me and use it as a hub that you connect to from various devices, then it's just much easier to find a place for it at home where it will not ruin the aesthetics. Also, it's much easier to take it with you while you travel.
That being said, everything what I will describe here can be also done with a cheap laptop running Linux, if you fancy that.

The first steps

The very first step as you might guessed it is to buy a Pi. I recommend getting it with an SD card that has pre-installed Raspbian on it.

Also, be careful as the standard package does not contain a power adapter. You can use it with more powerful phone charger if you have any extra laying around.
For the very first setup you will also need a display, keyboard and mouse.

Once you started up the Pi for the first time, connect with it to your local network. This can be either cable or through Wifi.
Then, start and configure RealVNC (comes with Raspbian) as it is a really convenient way to access you Pi, even remotely. Configuration is super easy, just register an account and follow the Wizard.
Once it's done, use VNC Viewer on any device to log in. (Windows, Mac OS, iOS, Android)

After this has been done, you basically don't need to connect any peripherals to the Pi anymore. Raspbian will detect if you start it up without any display, keyboard or mouse connected, and it will automatically boot into a so called header mode. In header mode, you can connect to the Pi through SSH or via RealVNC from any device: phone, tablet, laptop. And not just from your local network, but also remotely.

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
Screen Shot 2018-09-05 at 18.18.11.png
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 #! python3.
  • On OS X, the shebang line is #! /usr/bin/env python3.
  • On Linux, the shebang line is #! /usr/bin/python3.
    (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.
    RPi Terminal.png
  • 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:
    RPi Terminal crontab.png
    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.


Let's look at a few examples:

15 9 5 7 * /home/pi/yourscript.py will execute yourscript.py located in your home folder on every 5th of July, at 9:15 AM.

30 10 * * 1-5 /home/pi/yourscript.py will start yourscript.py on every weekday (from Monday till Friday) at 10:30 AM.

0 8 1-10 * * /home/pi/yourscript.py will start yourscript.py every day from the 1st till the 10th of each month, at 8:00 AM.

This is how the Crontab line looks like for my FoodNotifier.py:
0 11 * * 1-5 /home/pi/FoodNotifier.py
This will schedule it to run on every weekday at 11:00 AM.


Once you have added your line, hit CTRL+O to write out and hit Enter to save.
Exit crontab with CTRL+X

Other useful Crontab parameters

  • If you are using a script that's running or listening constantly, and you want it to shut down at a certain time you can do that with the killall -9 yourscript.py parameter.
    Example: 0 13 * * 1-5 killall -9 yourscript.py will stop yourscript.py at 13:00 PM, on every week day.
  • It's possible to shut down your Pi with the sudo shutdown -h now parameter.
    Example: 0 21 * * 6 sudo shutdown -h now will shut down your Pi on every Saturday at 21:00 PM.

Final words

I hope this short tutorial gave you an idea how easy it is to set up a Pi and start using it for Python script automation.
I think the possibilities are almost endless.
Like the Food Notifier example, you can set up scripts that automatically check information on the web every day and track changes or take action on them.
You can easily setup a python based chat bot that constantly runs, listens to inquiries and responds to them.
You can also hook up your Pi to a Home automation system and start / stop gadgets in your home periodically or remotely from a device.

All from a Credit card / PowerBank sized tool that can sit on a shelf next to your TV.

Discover and read more posts from Gergely Kovács
get started
post comments1Reply
sagar
2 years ago

Hello, i want to run multiple python scripts, but more than four scripts i can’t able to run in autoboot mode. so if you have any suggestions please let me know.