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.
into the command line.
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 test.py saved on your Desktop. If you want an example Python program to run then save the following as a Python program called test.py. When run this will create a new folder called test on your Desktop.
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.
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.
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 test.py saved on your desktop you would need to have
These two parts are separated by a space to make up the command.
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.
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.
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.
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.
Taking this further you could specify that you only want your program to run at 4:35 on the 12th March.
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.
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.
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
The 1,4 indicates the program will run on Monday and Thursday.
Using a - allows you to choose a series of values.
The 1-4 indicates the program will run Monday - Thursday. ie. Monday, Tuesday, Wednesday and Thursday.
Finally a / denotes increments of ranges.
This will run the program every 15 minutes.
One final useful command is
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.