Cron is a scheduling daemon that executes tasks (in the background) at specified intervals. These tasks are called cron jobs and are mostly used to automate system maintenance or administration.
For example, you could set a cron job to automate repetitive tasks such as backing up databases or data, updating the system with the latest security patches, sending emails, and so on.
Cron is named after Greek word “Chronos” that is used for time. This guide provides an overview of how to schedule tasks with crontab and how to using cron’s special syntax.
What is crontab file
Above all, a crontab file is a simple text file containing a list of commands meant to be run at specified times. It is edited using the crontab command. The commands in the crontab file (and their run times) are checked by the cron daemon, which executes them in the system background.
Each line of a crontab file is either “active” or “inactive”. An “active” line is an environment setting, or a cron command entry. An “inactive” line is anything ignored, including comments.
Blank lines and leading spaces and tabs are ignored. Lines whose first non-space character is a pound sign (#) are interpreted as comments, and are ignored. Comments are not allowed on the same line as cron commands, because they will be interpreted as part of the command. For the same reason, comments are not allowed on the same line as environment variable settings.
Cron command entries
Each entry in a crontab file consists of six fields, specifying in the following order:
minute hour day month weekday command
* * * * * command - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)
|Minute||0 – 59||11 * * * *||The cron job is initiated every time the system clock shows 11 in the minute’s position.|
|Hour||0 – 23||0 1 * * *||The cron job runs any time the system clock shows 1am (1pm would be coded as 13).|
|Day||0 – 31||0 0 8 * *||The day of the month is 8 which means that the job runs every 8th day of the month.|
|Month||0 = none and 12 = December||0 0 0 8 *||The numerical month is 8 which determines that the job runs only in August.|
|Weekday||0 = Sunday and 7 = Sunday||0 0 * * 1||1 in the current position means that the job would only run on Mondays.|
|Command||Special||The complete sequence of commands to execute. Commands, executables (such as scripts), or combinations are acceptable.|
Any of these fields can be set to an asterisk (*), which stands for “first through last”. For instance, to run a job every hour, put * in the Hour field.
Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an “hours” entry specifies execution at hours 8, 9, 10 and 11.
Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples: “1,2,5,9“, “0-4,8-12“.
Steps are also permitted after an asterisk, so if you want to say “every two hours”, you can use “*/2“.
Commands are executed by cron when the minute, hour, and month fields match the current time, and at least one of the two day fields (day of month, or day of week) match the current day.
The cron daemon checks the crontab once every minute.
Cron also offers some special strings, which can be used in place of the five time-and-date fields. They are essentially shortcuts for the equivalent numeric schedule specified:
|@reboot||Runs the specified command once, at start up.|
|@yearly, @annually||Both run the specified task every year at 12:00am on the 1st of January. This is equivalent to specifying “0 0 1 1 *” in the crontab file.|
|@monthly||Runs the job once a month, on the 1st, at 12:00am. In standard cron syntax this is equivalent to “0 0 1 * *”|
|@weekly||Runs the job once a week at 12:00am on Sunday. In standard cron syntax this is equivalent to “0 0 * * 0”|
|@daily, @midnight||Both run the cronjob every day at 12:00am. This is equivalent to specifying “0 0 * * *” in the crontab file.|
|@hourly||Runs the job at the top of every hour. In standard cron syntax this is equivalent to “0 * * * *”|
How to use crontab command
The crontab command creates a crontab file containing commands and instructions for the cron daemon to execute. You can use the crontab command with the following options:
|crontab -e||Edit your crontab file, or create one if it doesn’t already exist.|
|crontab -l||Display your crontab file.|
|crontab -r||Remove your crontab file.|
|crontab -u user||Used in conjunction with other options, this option allows you to modify or view the crontab file of user. Only administrators can use this option.|
Therefore, to edit or create your own crontab file, type the following command at the shell prompt:
There is no need to restart cron after changing the crontab file. Cron will examine the modification time on all crontabs and reload those which have changed.
How to edit crontab for a different user
To edit the crontab for a another user, enter the following command:
crontab -u other_username -e
Cron Job Examples
|* * * * * /root/script.sh||Run Cron Job every minute.|
|0 * * * * /root/script.sh||Run Cron Job every hour.|
|0 0 * * * /root/script.sh||Run Cron Job every day at midnight.|
|0 2 * * * /root/script.sh||Run Cron Job at 2 AM every day.|
|0 0 15 * * /root/script.sh||Run Cron Job every 15th of the month at midnight.|
|0 0 0 12 * /root/script.sh||Run Cron Job on December 1st at midnight.|
|0 0 * * 6 /root/script.sh||Run Cron Job on Saturday at midnight.|
|0 15 * * 1-5 /root/script.sh||Run Cron Job at 3 PM on every day from Monday through Friday.|
|*/5 * * * * /root/script.sh||Run Cron Job every 5 minutes.|
|0 8-16 * * * /root/script.sh||Run Cron Job every day, every hour, on the hour, from 8 AM through 4 PM.|
|0 4 * * 2,4 /root/script.sh||Run Cron Job at 4 AM on Tuesday and Thursday.|
|@reboot /root/script.sh||Run Cron Job when the system starts.|
You now have a good understanding of how to use cron to schedule tasks in Linux. Use the examples presented in this tutorial to create and schedule cron jobs on your system. When combined with shell scripts, you can automate tasks that are normally tedious or complicated.