BC Robotics

Setting Up A Cron Job On The Raspberry Pi

  PRODUCT TUTORIAL

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. 

Requirements:

This tutorial requires several items:

0%

Step 1 - Get Everything Prepared

In this short tutorial, we are going to get started with the assumption that you have already set up your Raspberry Pi, installed Raspbian, and all of the basic configuration on first boot has been done.  

8.3%

Step 2 - How It Works

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!  

16.7%

Step 3 - How To Create And Edit Cron Jobs

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. 

25%

Step 4 – Creating A Scheduled Task

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. 

33.3%

Step 5 - Minutes

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.

  • Inputting an “ * “ means “any value” and will run the code any minute – meaning your script would run every minute of the hour.
  • Inputting a number between 0 and 59 will run the script at a specific minute. An entry of 5 would mean your script runs 5 minutes into an hour.
  • Inputting a comma separated list of numbers like: 5,15,47 would result in your script running at minute 5, 15, and 47 of the hour.
  • A range of minutes can also be entered using a dash. 5-47 would result in your script running every minute between minute 5 and 47 of an hour.
  • A step value can also be set up. Using */5 would run your script every 5 minutes of an hour (the equivalent of entering 5,10,15,20,25,30,35,40,45,50,55) 
41.6%

Step 6 – Hours, Days, Months, Days of Week

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. 

50%

Step 7 – The Script To Run

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. 

58.3%

Step 8 – Assembling Example Cron Schedules

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.  

66.7%

Step 9 – The Script To Run

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! 

75%

Step 10 – Running On Boot

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 &
				
			
83.3%

Step 11 – Saving

Once you have made your entries, exit by pressing “CTRL + X” and hitting Y when prompted to save the file. Just keep in mind, they will run relative to your Pi’s date and time so be sure you have your time zone set correctly! 

91.6%

Step 12 – Viewing

If you wish to view your scheduled tasks without editing you can use the command:

				
					crontab -l 
				
			

This will display the file without opening it to edit.

And that’s all there is to it – now you can schedule a script to run anytime and interval with ease on your Raspberry Pi!

100%

13 thoughts on “Setting Up A Cron Job On The Raspberry Pi”

Leave a Reply

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare