Cron jobs are an easy way to schedule a script or program to run at specific dates and times in regular recurring cycles. These can be used for server maintenance tasks and scheduled backups, right out to real world applications for things like turning on or off lights and other automation. In this tutorial we will go through the steps of setting up a cron job on a Raspberry Pi along with some of the basic configuration options.
There are a lot of different operating systems out there for the Raspberry Pi, so we are going to focus on the most popular: Raspbian. We are using the version dated: 2019-7-10 (Pi 4 Compatible) available from the Raspberry Pi Foundations’ Download Page. You don’t need to use the Raspberry Pi 4, any Raspberry Pi will do. However, deviating from the recommended operating system version may result in different / additional steps so if you are very new to this, we recommend following the tutorial exactly.
Cron is a time based scheduler found in “Unix-like” operating systems (such as Raspbian). Just like setting up a notification or recurring notification in a calendar or scheduling app for day to day appointments, Cron allows you to schedule scripts and programs. This can be as simple as running something every 5 minutes, every hour of every day, to precise things like specific times on specific days only in specific months. Overall, a very useful tool to have!
Cron is a part of the Raspbian operating system and isn’t terribly difficult to work with. In older versions of Raspbian we had gnome-scheduler, a graphical editor for Cron Jobs. However, this hasn’t been pulled into Raspbian Buster at the time of writing this tutorial. To keep this compatible with the Raspberry Pi 4, we will go through how to set it up the old fashioned way! Start by firing up terminal and run the crontab command with the -e flag to edit the table of jobs:
You will be prompted to select an editor – we recommend using “nano”. Select it by entering the corresponding number, and hit enter to continue.
The editor should have now opened – so we can create our first scheduled task. Tasks are scheduled using a specifically formatted line containing six components. From left to right these are:
minute / hour / day of month / month / day of week / command to execute
0 12 * * 1-5 python3 /home/pi/test.py
This group of different parameters allows a lot of control as to when your code is executed. Each task is added to a new line in the cron table with these 6 components.
In the next steps we will go through each of these components before assembling what we have learned to create a few different example schedules.
Since minutes are the first component of a schedule we will go into depth on this one. Minutes can be entered as an asterisk (representing any/all), a number (between 0-59), a comma separated list of numbers, a range of numbers, step values, or a combination of these commands.
Hours are the second component of the schedule. Just like minutes, hours can be entered as an asterisk, a number (between 0-23), a comma separated list, a range of numbers, a step value, or a combination of the above. We wont go into the same level of detail as above in this example as the same principles apply to hours, days, months, and day of week as they do to minutes.
The third component is Days of a Month. This component accepts numbers between 1 and 31. In months where there are less than 31 days, numbers above the number of days are ignored. Days of a month can be used to run programs on odd or even days (like our irrigation system) using a step value within a range value. Example: Odd Days would be entered as 1-31/2 (every second day of the month starting with 1) while even days is entered as 2-30/2 (every second day of the month starting with day 2)
The forth component is Months. Just like minutes and hours, months can be entered as an asterisk, a number (between 1-12), a comma separated list, a range of numbers, a step value, or a combination of the above.
The fifth component is Day of the Week. This is a bit different as it allows you to run a daily script on a specific day of the week rather than a the day of a month. If you wanted to run something on Mondays or Sundays regardless of what number the day is, this would be where you enter it. Just like every previous component, days of the week can be entered as an asterisk, a number (between 0-6), a comma separated list, a range of numbers, a step value, or a combination of the above. The week starts on Sunday (0) and goes to Saturday (6). Entering 7 will work, however it is non-standard. It is the same as entering 0 and represents Sunday.
The final component is the script that you want to run. In our example we are going to run a python 3 script called “test.py”. We have saved this in the default directory of /home/pi/.
We would enter component six as:
The script can be anything you can run in the terminal and is not limited to python programs or other scripts, replacing your command with: sudo reboot now would make your Pi restart anytime the scheduled task runs.
Now that we have gone over all of the different components, we can look at a few examples:
* * * * * python3 /home/pi/test.py
This would run the script every minute of every hour of every day of every month (every minute, 24/7)
0 0 * * * python3 /home/pi/test.py
This would run the script at minute 0 and hour 0 of every day of every month (midnight, daily)
15,45 7-18 * * 1-5 python3 /home/pi/test.py
This would run the script at 15 and 45 minutes past the hour, every hour between 7am and 6pm, on every day of the week from Monday to Friday.
0 20 1-31/2 5-9 * python3 /home/pi/test.py
This would run the script 8pm every odd numbered day from May to September.
Since you don’t necessarily want to wait around for weeks or days to make sure your code will run at the right time… there is a handy “cron calculator” available online to test your schedules. Head on over to https://crontab.guru and enter your command in (without the script portion) and it will decode the schedule as your Pi (or other device) will see it. This tool is very useful for testing and creating complex schedules!
With the Pi, a non-standard command can be used to run a program as soon as the Pi boots up. This is an alternative method to our tutorial: Running a Python Program on boot with the Raspberry Pi.
Entering this on a line will run our same script only when the Pi Boots:
@reboot python /home/pi/test.py
However, if this is a continuously running program and not a simple script, this will block the Pi from fully booting. To run your command in the background while the Pi boots up and runs normally, add “ &” to the end of the command like so:
@reboot python /home/pi/test.py &