Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

Linux system automation with at and cron (crontab)

Automating tasks

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.

crontab on linux Raspberry Pi konsole

Two common programs are at and cron which are used to schedule one-off tasks and regularly recurring tasks respectively.

Running at a specific time with at

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.

Time format

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 at commands

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

Running regularly with cron and crontab

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.

The below video includes a section on the use of cron and crontab. The video will start at the cron section of the video.

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):

  1. m - Minutes - 0 to 59
  2. h - Hours - 0 to 23
  3. dom - Days of Month - 1 to 31
  4. mon - Month - 1 to 12 or JAN-DEC
  5. dow - Day of week - 1 to 7 or MON-SUN (or 0 can be used for Sunday if preferred)

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

cron.d / cron.daily

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.

Alternatives to Cron

Anacron - cron for "normal" computers

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.

Systemd

An alternative to using cron is to use the scheduling features in systemd. Cron can still be used on systems running systemd so you can continue to use the standard cron / crontab if preferred.

For more details see guide to automatically starting programs and applications on a Raspberry Pi.

Previous Starting applications automatically in Linux
Starting applications automatically in Linux
Next Introduction to Linux security
Introduction to Linux security