Monday, 9 June 2014

Scheduling Python Programs using Cron

Cron has become one of my favourite utilities in Linux. It allows you to schedule tasks to automatically happen at certain times. It is one of those tools that is so simple and so powerful, but all too often overlooked, or not known about.

I first stumbled across Cron when I decided to set up and administer a wiki server for the company I work for. There is a lot of data on the wiki and it is important that it gets backed up regularly.

Initially I would take a dump of the MySQL database, and then copy the files over on a daily basis by hand.

I then wrote a Python script which carried out these tasks for me. I just had to remember to run the script on a daily basis.

Finally I automated this using Cron.
  • Now every morning (Monday - Friday) at 1:00 am the company wiki is automatically backed up to two locations.
  • Secondly on the 1st of every Month at 2:00 am the wiki is automatically backed up to two different locations.

All this is done without me ever having to worry if it has been done. Cron looks after it all for me.

What is more is it is very simple to use.

This tutorial will help you get to grips with Cron and have your Raspberry Pi or any other Linux computer working away for you in the background.

Firstly Cron does require the time on your Linux computer to be correct, otherwise tasks will not happen when you expect them to! Not that such a basic mistake would ever catch me out while writing this tutorial...

So if you are using a Raspberry Pi, it might be worth setting the time at this point.


sudo raspi-config

into the command line.

Then choose

Internationalisation Options > Change Timezone > Europe > London

for those of you in the UK. Obviously everyone else choose a location to suit your local timezone!

For the purposes of this blog post we will assume you are wanting to run a Python program called saved on your Desktop. If you want an example Python program to run then save the following as a Python program called When run this will create a new folder called test on your Desktop.

import os


Now again in your command line we will edit a file called crontab, this is short for Cron Tables, a place where you store all your actions.

To edit crontab type the following into the command line.

sudo crontab -e

This will bring up a text file which looks as follows.

The majority of the text in this file briefly explains Cron and how to use it. The first thing you will notice is that all lines start with a #. This means these lines are ignored by Cron, they are for your reference only.

While there are some basic instructions included in this file the most useful line is the bottom line.

# m h dom mon dow command

These are all explained in the file to mean the following.
  • m - minute
  • h - hour
  • dom - day of month
  • mon - month
  • dow - day of week
  • command - This is the command you want to run.

Lets start with the command, as that's the easy bit.

If you want to automatically run a Python program the command is made up of two parts.

On the Raspberry Pi, if you are using Python 2.7, the first part of this will be as follows.


or for Python 3


The second part is a link to the Python program. For a program called saved on your desktop you would need to have


These two parts are separated by a space to make up the command.

/usr/bin/python /home/pi/Desktop/

These other parameters (m h dom mon dow) are what you will use to specify when your task will run. For each of them you can specify a number or if you want to use all possible values a * is used to signify this.
  • The minutes can be numbers from 0 - 59 and indicates the minute in the hour you want the task to run.
  • The hours can be from 0 - 23 and indicates the hour you want the task to run.
  • Day of the month, 1-31, indicates the date within the month in which you would like to run the task.
  • Month, 1-12, indicates in which month you would like to run the task.
  • Day of the Week, 0-6, indicates on which day you would like to run the task. 0 is Sunday, 1 is Monday etc. 
The secret to setting this correctly is to use a combination of these to define the schedule.

If you wanted to run your python program every hour, then you would only have to supply a value for the minutes, specifying at what point in the hour you wanted your program to run. For example to run your program every 35 minutes past the hour you would enter the following into your crontab.

35 * * * * /usr/bin/python /home/pi/Desktop/

The hours and minutes are easy to work out. If you want to run something everyday at 4:35 you would simply create a line in your crontab which would say.

35 4 * * * /usr/bin/python /home/pi/Desktop/

Making this run on the 12th of the month only you would need to put the figure 12 into the day of the month field also.

35 4 12 * * /usr/bin/python /home/pi/Desktop/

Taking this further you could specify that you only want your program to run at 4:35 on the 12th March.

35 4 12 3 * /usr/bin/python /home/pi/Desktop/

Alternatively you may want to to run at 4:35 every Monday, so would need to use the Day of the week field. The numbers from 0 - 6 represent the days of the week. 0 is a Sunday, 1 is Monday etc.

35 4 * * 1 /usr/bin/python /home/pi/Desktop/

We have put the Day of the Month and the Month back to being a * for this to happen, as we don't want to limit when our program to run on the 12th March only. Leaving these values in would only run the program on the 12th March at 4:35 if that day actually happened to be a Monday! The * implies it can run any day or month.

Easy huh?

There are a few other special characters which help you define when your program should run.

A comma can be used to allow you to add more values into a field. For instance

35 4 * * 1,4 /usr/bin/python /home/pi/Desktop/

The 1,4 indicates the program will run on Monday and Thursday.

Using a - allows you to choose a series of values.

35 4 * * 1-4 /usr/bin/python /home/pi/Desktop/

The 1-4 indicates the program will run Monday - Thursday. ie. Monday, Tuesday, Wednesday and Thursday.

Finally a / denotes increments of ranges.

/15 * * * * /usr/bin/python /home/pi/Desktop/

This will run the program every 15 minutes.

One final useful command is

@reboot /usr/bin/python /home/pi/Desktop/

Will run the program when you reboot your Raspberry Pi or Computer.

From these few examples you have seen that Cron gives you a huge amount of flexibility on when you schedule tasks.

I hope you have found this blog post useful and that it will help you get started automating your programs.