Schedule jobs with cron

The cron program in Linux is used to perform tasks at regular intervals. It is usually used for system maintenance tasks, such as log rotation or deletition of unnecessary files from the /tmp directory. The cron program wakes up every minute, checks its configuration files and executes commands specified in the configuration files if the time is right.

Commands executed by cron are known as cron jobs. Two types of cron jobs exist:

  • system cron jobs – tasks run as root, usually used for system maintenance tasks.
  • user cron jobs – tasks created by users to run their programs.

Creating system cron jobs

To create system cron jobs, you need to modify the /etc/crontab file. It is a simple text file with a list of commands that need to be executed at certain times:

etc crontab

Consider the following line from the output above:

00 21 * * * root rm /home/bob/trash/*

A line in a crontab file consist of sections separated by spaces. The syntax of a cron job is:

  • first field – minute(s) (possible values: 0-59)
  • second field – the hour (0-23)
  • third field – the day of the month (1-31)
  • fourth field – the month (1-12)
  • fifth field – the day of the week (0-7, both 0 and 7 can be used for Sunday)
  • sixth field – the account that will be used to execute the command
  • seventh field – the command that will be executed


Note that the asteriks (*) character matches all possible values. Commas separate items of a list (e.g. 0,3,5 matches any of these values). Hyphens (-) define a range (e.g. 8-15 in the hour field specifies the time from 8:00 to 15:00).

In our example the command rm /home/bob/trash/* will be run by root at 21:00 every day of the week, every month.

Geek University 2021