Cron, a name derived from “Chronos”, the greek word for “time”, is a utility program for UNIX-based operating systems like Linux. Essentially, Cron is a time-based process scheduler used to automatically run repetitive commands at fixed times. In other words, Cron makes things happen by themselves at specified times. These things, or commands, are known as “Cron Jobs”.
While traditionally used for automating system maintenance and administration (monitoring disk space, automating backups), Cron can be utilized for a wide variety of other purposes: automatically downloading files and emails, downloading software updates, sending reports and invoices or other possibly more creative uses.
Cron jobs are composed of both Cron expressions and the command to run those expressions. Therefore, scheduling tasks in Cron requires one to master the great art of writing Cron expressions.
Cron Expressions are, in most cases, a string of five fields, which make up the building blocks of every Cron expression. Each field represents a specific time unit, so that the different fields represent minutes, hours, days of the month, months, and days of the week respectively:
Fields can be comprised of the following values:
Single inputs are basic numeric values (or names of days or months) which designate a specific time. For example,
2 in the hour field would designate 2 am.
Ranges consist of two values separated by a dash. Every single value between these two values is represented by the range. For example, a range of
0-30 expresses all the numbers between 0-30, including both 0 and 30.
Any - *
An asterisk (or a star) -
* - represents all the possible values in a range for the field. For instance, an asterisk in the “minute” field represents all the possible inputs for said field, or in other words, every minute.
The range/step is made up of a range and a “step” value separated by a slash (e.g.
0-30/6). An asterisk can also be used instead of a range to represent all the possible inputs in the field (e.g.
*/5). Every step represents a value to skip within the range:
*/5 in the minute field means every 5 minutes,
0-30/6 means every 6 minutes between minute 0 and 30 - that is, minutes 0, 6, 12, 18, 24 and 30.
Lists are a series of different values or ranges separated by commas (e.g.
0,10,30-35). Lists come in handy when trying to schedule tasks for certain times. If you wish to schedule a task for, let’s say, both Sunday and Thursday, then you could use a list. Remember not to add spaces between values and commas as space is used to separate between the different fields.
Cron expressions are designed so that there is a certain number of valid inputs for every field. In other words, There is a limited number of values that Cron is able to comprehend. These values change according to the field in which they are written:
The Minute field is the smallest time field available in Cron. It can receive any input ranging from 0 to 59, with each input representing a specific minute. For example:
* * * * * - Every minute
*/15 * * * * - Every 15th minute (i.e. xx:00, xx:15, xx:30, xx:45). This can also be expressed by using a list:
0,15,30,45 * * * *
Any input from 0-23 is valid in the Hour field. For example:
0 * * * * - Every hour
45 * * * * - Hourly, at 45 minutes past the hour
0 0 * * * - Every midnight
0 */2 * * * - Every two hours
Day of Month
Any input ranging between 1-31 would work in the Day of the Month field. This field designates the days of the month on which the job will run. For example:
0 9 10,15 * * - At 9am on every 10th and 15th of the month
Both JAN-DEC and their numeric counterparts, 1-12, are valid inputs in the Month field. This field schedules the job to run only on the assigned month or months, in conjunction with the other fields. More examples follow:
* * * 4 * - At every minute in April
0 9 1 */2 * - At 9AM on the first day of every second month
Day of Week
As in the Month field, the Day of Week field has two possible types of values: 0-7 (with both 0 and 7 representing Sunday) and SUN-SAT. To illustrate the usage of the last field:
0 9 * * 1-5 - Every weekday (Monday through Friday) at 9AM
0 9 * * 6,0 - Every weekend - Saturday and Sunday at 9AM
However, a thing to note about both the Day of Week and the Day of Month fields is that the Cron job will be triggered if one of these fields is true, even if the other is not.
0 0 13 * 5 stands for every Friday and every 13th of the month. This feature is completely unique to the Day of Week and Day of Month fields.
0 9 1 */3 * - Every quarter - on Monday at 9AM
0 9,21 * * * - Twice a day - at 9AM and 9PM
*/5 9-17 * * * - Every 5 minutes, during office hours - 9AM-5PM
And in case you ever feel like confusing your colleagues, Cron also allows for some completely unnecessarily complicated commands:
*/5 */2 13 4,8 0,1,2,3,4,6 - Every 5 minutes, every other hour, on every day excluding Friday, and also on the 13th of the month, in April and August.
Why use Cron
Cron is an accurate, flexible, all purpose solution for automating and scheduling tasks. Its preciseness is what sets it apart from other process schedulers. If you're looking for a reliable and scalable, fully managed cloud scheduler, check out Cron To Go now!