Running Things Regularly - cron
Last week I showed you how to run something each time your Raspberry Pi boots. This is not the only way; there is also 'cron'. But cron is actually a more powerful command - it allows you to run anything at a regular interval, be it every minute, hour, day, month or day of the week!
'cron' is short for 'chronograph', or 'clock'. It is a daemon that allows you to schedule commands to run at specific times. These events are listed in what is known as the 'crontab' file, which is short for 'cron table'.
To see what is already scheduled to run, open up a Terminal Window and run the following:
which, unless you have already set something up, will contain just comments:
# Edit this file to introduce tasks to be run by cron.
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# For more information see the manual pages of crontab(5) and cron(8)
# m h dom mon dow command
This will list only the commands scheduled when the current user is logged in. If you want to see what is run by the root user, use:
sudo crontab -l
To edit what is in the crontab file, use:
sudo crontab -e for the root user).
This will open the crontab file in the nano editor. The format of each line must be in the format:
m h dom mon dow command
Where each parameter is separated by a space, with the following values:
mis the minute (between 0 to 59)
his the hour (in 24 hour format, between 0 and 23)
domis the day of the month (between 1 and 31)
monis the month (between 1 and 12, or three letter short code - jan, feb, mar etc)
dowis the day of the week (between 0 and 6, where 0 is Sunday, 1 is Monday etc, or three letter short code - sun, mon, tue etc).
commandis the command to be run
As well as single numbers for each of the first 5 parameters, you can also use the following special formats:
- A sequence of numbers, separated by a comma - e.g. 0,15,30,45
- A range - e.g. 4-8
- A sequence of ranges - e.g. 0-15,30-45
- An asterisk, meaning 'all' - e.g. *
- Every n'th time by adding the /c suffix - e.g. */5 for every 5th minute.
Note: You cannot use the three letter short codes in ranges.
* * * * * command- runs every minute
30 * * * * command- runs every 30 minutes
30 9 * * 5 command- runs every Friday at 9:30am
The command itself can be any shell command - i.e. whatever you type into the terminal window, or 'shell'. The following simple cron job writes the current date to the file 'file' in your home directory every minute:
* * * * * date >> file
You can watch the file being updated each minute with the following command:
tail -f ~/file
You can also use the special time macro of '@reboot', which runs the command every time your Raspberry Pi reboots. For example:
@reboot echo 'Rebooted' >> ~/file
You can even run Python files using the commands in the same way you would run the commands from the command line, so you could, for example, attach an LED (via resistor) and get it to flash every hour. Your imagination is the limit!