- Learn Linux
- Learn Electronics
- Raspberry Pi
- LPI certification
- News & Reviews
When running a linux system then it is useful to run tasks at a certain time or regularly. This can be useful for regular backups, performing regular checks on the system or run your own programs automatically.
Two common programs are at and cron which are used to schedule one-off tasks and regularly recurring tasks respectively.
The at command can be used to queue tasks to run at a time scheduled in future. There is also a closely related command called batch, which allows commands to be run when the system load is low. The batch command was often used in the past when computers were less powerful and many users would share a single computer, but it is less useful today so this will concentrate on the at command.
The easiest way to explain the at command is with an example. The following example will reboot the computer one hour in the future:
echo reboot | at now + 1 hour
The at needs two pieces of information, the first is the time to execute the command (which supports various different time formats examples below) and the second is the command to execute. The time is included in the command line arguments (for this example "now + 1 hour"), but the command to execute comes from stdin (typically sent to the at command using echo as shown above).
at runs the command using the login details and environment variables taken from when the at command is run.
There are several different ways that the time can be entered using natural language. The example above is for a time relative to the current time, so "now + 1 hour" translates to one hour after the current time. This could be represented by days, weeks, months or even years. Now can be replaced with a different time value for example "5 pm + 1 day" will run the command at 5pm tomorrow.
Another way to specify the time is by entering the time directly eg. "9:30 AM" or "0930", or to include the full date and time for example: "1:00 PM 25.12.2014" (note that the dot uses the standard date used in most countries DD.MM.YYYY, replacing the '.' character with '/' will use the MM/DD/YYYY date format used in the USA).
There are also some common terms that can be used directly such as "noon" for 12:00 pm, midnight for 12.00 am and teatime for 4:00 pm.
Other commands that can be used to manage the at jobs are: atq to list the pending jobs and atrm to delete jobs (using their job number available from atq).
Cron is a scheduler that can run commands at regular intervals. It's often referred to as crontab which is name of it's configuration file and the tool used to edit the configuration file.
Each user can have their own list of cron tasks. These can be edited using
crontab -e which will load the current crontab entries into the default editor.
# m h dom mon dow command 0 9 * * 6,7 wget http://127.0.0.1/switchon?socket=1 30 15 * * 1-5 wget http://127.0.0.1/switchon?socket=1 0 11 * * * wget http://127.0.0.1/switchoff?socket=1
The list above shows an example crontab edit session. I have dropped the comments at the top of the file except for the line showing the formatting of the command.
Each line reflects a single entry which has 5 time/date fields to specify when the commands are run followed by the command to execute. Using the abbreviations above these are (in order):
The fields can have a single value, comma separated values, range of values or and asterix for any value.
To interpret the top entry above - this will run on the 0 minutes of the 9th hour (9:00 am) on any day of the month and any month of the year, but only on days of the week 6 and 7 (the weekend). The second entry will run at 15:30 on each day of the month and any month of the year, but only on weekdays (1 to 5). The third entry runs at 11:00 every day.
As well as the time based entries there are other special commands that can be used such as @weekly (midnight on Sunday morning), @hourly once an hour and @reboot which runs when the system is started (on some distributions @reboot may also be triggered if cron is restarted without a reboot, although cron is not normally restarted on a running system).
On Debian based distributions there are also directories such as /etc/cron.d, /etc/cron.daily etc. which can be used as to schedule tasks. I would personally recommand using crontab -e to create cron tasks but you can run
man cron for more details about the various cron directories.
Cron is designed for server computers and assumes that the computer will be running whenever the scheduled time is reached. This does not work as well on a desktop or laptop computer where the system may be switched off when the command is scheduled to be run. An alternative is anacron which works in the same way as cron, but where the computer was switched off when a task was scheduled to be run then anacron will run it when the computer is next switched on.
As I write this many Linux distributions are going through a big transition from the traditional init daemon based system startup process to a new systemd system. The systemd process allows for much more control than the old system and as such can be used in place of cron and crontab. The new systemd startup will be detailed in a future linux guide. Cron can still be used on systems running systemd so you can continue to use the standard cron / crontab if preferred.