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.

type

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 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.

import os

os.chdir("/home/pi/Desktop")
os.makedirs("test")

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.

/usr/bin/python

or for Python 3

/usr/bin/python3

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

/home/pi/Desktop/test.py

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

/usr/bin/python /home/pi/Desktop/test.py

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/test.py

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/test.py

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/test.py

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/test.py

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/test.py

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/test.py

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/test.py

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/test.py

This will run the program every 15 minutes.

One final useful command is

@reboot /usr/bin/python /home/pi/Desktop/test.py

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.






13 comments:

  1. Hi Trevor, thanks for much. Keep up the great work!

    ReplyDelete
  2. stomausa Welcome to Stoma USA.
    Dear customer

    Leaf through our brochure „select“ and you will find a wide selection of standard and special instruments as well as a comprehensive system for organizing instruments; as usual, only an excerpt of the complete stoma® range. Various well thought-out instrument concepts, usually compiled by recognized specialists for their treatment areas, help you to put together handy instrument sets.

    ReplyDelete
  3. I didn t know there was a place like this. I think it s good because there are many good comments. Let s get along well from now on. 먹튀검증업체

    ReplyDelete
  4. It was very easy to find my way around and very user friendly Good job to your designer and dev of your site. I hope your rate was doing well. 메이저사이트

    ReplyDelete
  5. It’s good to have a website like this. I can read all the opinions of others as well as i gained information to each and everyone here on your site. Just keep on going dude.
    Check over here: 토토사이트

    ReplyDelete