How to automate run your python script in your Raspberry Pi
By: Sasiwut Chaiyadecha
Tags: Crontab, Linux, Raspberry Pi, Python, Task Scheduler

How to automate run your python script in your Raspberry Pi Sasiwut Chaiyadecha Follow Jul 7 · 4 min read

Hello everyone, today we are going to talk about the automation tasks. Many of yous might own the python script that help you to do somethings. I have one. My python script is a daily COVID-19 bot. It is simply to push the notification message for daily cases to LINE application and my Twitter account. Just better to know the situation, isn’t it?

Well, I don’t want to run it manually everyday because it might far away from the word “bot” if I doing so. I decided to keep it into my single board computer, I own one called Raspberry Pi. It also run 24/7 so, it is a good place.

I assumed all of you have Linux installed in your single board computer. In the Linux has a tool for task scheduler, called “Crontab”. Today, we are going to run our python script by using crontab.

Introduction

It is better to know about crontab

First, we can simply access to crontab by open the terminal and passing command below:

crontab -e

(If you run for the first time, it might ask you to select to editor. I would recommend Nano or VIM. It is easy to use.)

After you have your editor open, it is showing you how the command work. It can be summarised as below:

* * * * * command to be executed

- - - - -

| | | | |

| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)

| | | ------- Month (1 - 12)

| | --------- Day of month (1 - 31)

| ----------- Hour (0 - 23)

------------- Minute (0 - 59)

1st * is minute range between 0–59

is minute range between 0–59 2nd * is hour range between 0–23

is hour range between 0–23 3rd * is day range between 1–31

is day range between 1–31 4th * is month range between 1–12

is month range between 1–12 5th * is weekday range between 0–6 (0 is Sunday and 6 is Saturday)

is weekday range between 0–6 (0 is Sunday and 6 is Saturday) 6th * is command to run the script

For example, if you want to do somethings at 10.45 everyday, you can write the command as:

45 10 * * * yourcommand

For someone who get the difficulty and not sure to address the command’s syntax, you can use the website below to get the guild idea.

Or if you want to do inversely by picking the date and time and need command to be generated, you can visit the site below:

Python script task schedule

Now, we all know how the crontab work. Let’s integrate it with our python script. First, I am going to create simple ‘hello world’ python script and save it in the desktop. Go to terminal and cd Desktop . Then, I use Nano to create python script as nano pytest.py . In the nano editor, I code the classic one.

print('hello world')

Pressing ‘Cmd+x’ or if you are the Windows guys, use ‘Ctrl+x’. Then, you can press ‘y’ to save and exit the editor. Let’s try to run the code, open terminal and type python3 pytest.py . The terminal should display something like this:

hello world

It works fine! Now, we can add the python script to crontab. However, before doing that we need to know where is our python executor. In Linux, we can use command which python3 (I am using python3) to find the path of executor. Normally, it should locate in the path below:

/usr/bin/python3

We are ready to automate the python script. The one last step that you probably should know about crontab. The crontab doesn’t show any output in the terminal. To check whether your task schedules still working or not, you could create the log file to keep results. It can be done by simply use >> syntax to the log file as the example below:

>> /home/pi/Desktop/log.txt

The command above will create text file, named ‘log’ in your desktop. It can contain any output from your python script. In this case, it is going to append ‘hello world’ every time when the script is running. You can refer to full command below:

* * * * * /usr/bin/python3 /home/pi/Desktop/pytest.py >> /home/pi/Desktop/log.txt

When you can ensure that your crontab is working, you might not need to create the log.txt . You can only keep * * * * * /usr/bin/python3 /home/pi/Desktop/pytest.py for your task scheduler.

That’s all! Just simple and useful. See you next time.

You could find me on LinkedIn as link below: