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.
This tutorial requires several items:
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:
crontab -e
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
Example:
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:
python3 /home/pi/test.py
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 &
11 thoughts on “Setting Up A Cron Job On The Raspberry Pi”
Kieran Cranley
Hi when I put the following command into my Raspberry Pi terminal:
/usr/bin/scrot /home/pi/Pictures/pic.png
it works.
However, when I put the following line into crontab:
* * * * * /usr/bin/scrot /home/pi/Pictures/pic.png
it doesn’t work.
Can you help please?
Many thanks
mike87
might be a permission or maybe length of the command?
JASONXHI
The command is running under it’s own shell and desktop launched by cron, so it doesn’t have access to your desktop.
Mohit
I would like to know this as well. I think you need to specify the exact path for the cron to find the python or something of this sort. I also want to know how to do this
Nigel
Hi your above instructions are great, I have been trying to set crontab in the same manor for days now by putting the following line “@reboot Sudo Python3 /home/pi/Desktop/gpio.py &” this file is a python script, sat on the desktop that sets the GPIO BOARD pins 11 and 16 to output hi, running it in python on a pi 3b+ works perfectly to light 2 no LED’s. I cannot get crontab to work, it appears to save the edited file and confirms saving in the terminal once nano quits but when I reboot nothing happens and after reboot running crontab -e again and the edited line has been removed.
can someone please help
graeme milne
I have a cron script to startup a program in raspberry pi when rpi powers up.Can i add a script to shutdown the program before rpi shutsdown on the same script file or do i need to create another cron script ???
Thank you in advance for any answers.
Go easy with the replies as i am a novice .
Caramel13
Hello,
Is it an order for the script to be started up ?
I put (line 1) a command at 0 0 * * * (every day at midnight) and (line 2) another one at * * * * * (every day each minute), and I need the first script must run before the second one.
But It doesn’t >> in the cron.log, I can see the second starts a few seconds before the first.
Have a solution ?
Thanks for reply.
JFG.
Mark
Can you enter a string instead of calling a file? What about a simple Sudo command like
sudo apt-get update -y && sudo apt-get upgrade -y && sudo apt-get autoremove -y && sudo apt-get autoclean -y && pihole -up
No need to prep with a Python 3 call. Yes?
Chris @ BCR
Hi Mark – that is correct, if you are not calling a Python script, you don’t need the Python call
Steve Henderson
I’m very much a RaspiOS newbie and I’ve been fighting this for what seems like forever.
I have:
0 0 * * * sudo apt-get update; sudo apt-get dist-upgrade -y; sudo apt-get autoremove -y; sudo apt-get autoclean; pihole -up; pihole updateGravity; exit
but no part of it EVER runs automatically.
I’m sure I’ve overlooked something incredibly simple (for someone that has a clue–I do not) so explain
John
_Try simplifying it and get some feedback.
Take the first command and direct the output to a text file: ‘sudo apt-get update > /tmp/cron.log 2>&1’. Pick a location that suits you.
That will give you a text file with the std and err output. Once you work out what to do about that move onto the second command and so on.