Cron Expressions Explained

January 4, 2024

Introduction

System administrators spend considerable time performing routine maintenance tasks, such as creating backups, clearing cache, or updating software.

Automating these repetitive tasks reduces workload, eliminates human errors, and is essential for managing large, distributed server systems.

Learn about cron expressions and use them to execute commands or scripts at predetermined intervals.

cron expressions explained

What Is a Cron Expression?

Cron jobs are scheduled tasks used to automate repetitive processes. A cron expression is a string of characters that specifies the cron job schedule by defining exact times, dates, or recurring patterns.

A cron expression usually has five standard fields that represent time intervals like minutes, hours, or days, which dictate when the cron job will be executed.

Cron Expressions Overview

Cron expressions consist of time interval values and special characters. The special characters allow you to define complex schedules more concisely instead of typing lengthy value strings.

Fields in a cron expression must be written in a specific order. Otherwise, the cron daemon may not recognize or execute the cron job correctly.

The following table lists crone expression fields in the correct order, their allowed values, and allowed special characters:

FIELD NAMEALLOWED VALUESALLOWED SPECIAL CHARACTERS
Second0 - 59* - , /
Minute0 - 59* - , /
Hour0 - 23* - , /
Day of the Month1 - 31* - ? L , W
Month1 - 12 (or JAN - DEC)* - , /
Day of the Week0 - 7 (or SUN - SAT)* - ? L , #
Year1970 - 2099* - , /
*Second and Year fields are optional. Only specific extended cron versions support them.

Allowed Fields

Cron expressions are composed of different time interval fields, each separated by a single space. There are five standard fields, with two additional optional fields in some cron implementations:

Standard cron expression fields include:

  • Minute. The minute within the hour.
  • Hour. The hour of the day.
  • Day of the Month. The day in the month.
  • Month. The month of the year. The Month field value can be a number or the month abbreviation.
  • Day of the Week. The day in the week. The Day of the Week field can be a number or the day abbreviation.

Optional cron expression fields include:

  • Second. The second within a minute.
  • Year. The year for executing a task.

In the following cron expression brackets denote optional fields:

[SEC] MIN HOUR DOM MON DOW [YEAR] CMD 

The command (CMD), usually a script path or system command, is appended to the expression.

Allowed Values

Each cron expression field has a predefined value range. If a value is set outside this range, the cron daemon might ignore the job or respond with an error. The allowed field values in cron expressions include:

  • Second: 0 - 59
  • Minute: 0 - 59
  • Hour: 0 - 23
  • Day of the Month: 1 - 31
  • Month: 1 - 12 or JAN - DEC
  • Day of the Week: 0 - 7 or SUN - SAT. Both 0 and 7 represent Sunday.
  • Year: 1970 - 2099

For example, a task that needs to be completed at 10:15 AM on January 22nd would use the following expression:

15 10 22 1 * 

This sets the minute to 15, hour to 10, day of the month to 22, month to January (1). The Day of the Week is represented by the * special character to indicate any day. There is no need to set a date and to specify a day of the week in the same expression.

Allowed Special Characters

Special characters are used instead of or in conjunction with allowed field values to simplify and shorten expressions.

Certain special characters only work with specific fields. The following table lists special characters and what they are used for:

SPECIAL CHARACTERDESCRIPTIONAPPLICABLE
FIELDS
*The asterisk substitutes all possible values for a field. For example, when used in the hour field, it indicates that the task should be executed every hour.All
-The dash is used to define a range of values in a single field. For example, entering 3-5 in the minute field instructs a cron job to run every minute between and including the 3rd and 5th minute.All
/The forward slash is used to specify increments in a field. For example, */10 in the minute field means the task runs every 10 minutes.Second, Minute, Hour, Month, Year
?The question mark is used as a placeholder in the Day of the Month and Day of the Week fields when you want to specify one of these fields while leaving the other unspecified.Day of the Month, Day of the Week
LRepresents the last day in the Day of the Month or Day of the Week fields.Day of the Month, Day of the Week
WRepresents the weekday closest to a given day in the Day of the Month field. This character is useful for ensuring tasks only run on weekdays.Day of the Month
#The hashtag is used to specify the nth occurrence of a day of the week in a month. For example, 5#3 in the Day of the Week field runs a cron job on the third Friday of the month.Day of the Week
,The comma allows for specific, non-sequential time points to be defined within the cron schedule. Using 15,45 in the minute field runs the task at 15 and 45 minutes past the hour.All

Special Strings/Macros in Cron Expressions

Special strings are convenient shortcuts for defining commonly used cron expressions. You can use these readable special strings instead of typing full expressions:

  • @hourly. Replaces 0 * * * * to run a cron job every hour at the beginning of the hour.
  • @daily or @midnight. Use instead of 0 0 * * * to execute jobs at midnight.
  • @weekly. Instead of typing 0 0 * * 0, use @weekly to run jobs every Sunday at midnight.
  • @monthly. Equivalent to 0 0 1 * *. Use it to run a job at midnight on the first day of the month.
  • @yearly or @annually. Use instead of 0 0 1 1 * to execute a cron job every year on January 1st at midnight.

Cron Expression Examples

The following cron expression examples outline frequently used scheduling patterns.

To run a cron job every minute:

* * * * *

Execute a task every 15 minutes:

*/15 * * * *

Run a job on the 1st and the 30th minute of every hour:

0,30 * * * *

To execute a task at the start of every hour:

0 * * * *

Schedule a task to run every day at midnight:

0 0 * * *

Run a task every hour from 9 AM to 5 PM:

0 9-17 * * *

Execute a cron job on the first day of every month at midnight:

0 0 1 * *

Run tasks once a year on January 1st at midnight:

0 0 1 1 *

Schedule tasks at midnight on workdays:

0 0 * * 1-5

Run a task twice a year, on June 1st and December 1st at midnight:

0 0 1 6,12 *

Execute a cron job on the first Wednesday of every month at midnight:

0 0 1-7 * 3

Schedule a cron job at 5 PM on the 15th of every month. The W special character ensures the task is executed on the next workday if the 15th is on the weekend:

0 17 15W * *

Note: W and L special characters are available in advanced scheduling systems. Ensure your cron implementation supports these characters before using them.

Cron Expression Precautions

Cron jobs that miss their intended schedule can cause unexpected issues like a system using its resources inefficiently or forgoing critical updates. There are several precautions to take to ensure their automated tasks execute as intended:

  • Test Cron Jobs. Before deploying cron jobs in a production setting, test them in a staging or development environment.
  • Implement Logging. Cron daemons generate log entries for start and completion times but do not log error information by default. To track detailed error logs, manually configure cron jobs to redirect error output to a separate log file.
  • Balance Resource Usage. Schedule resource-intensive tasks at different times to ensure they do not overlap and that your system maintains optimal performance levels. This is especially important when scheduling long-running jobs.
  • Maintain Data Security. When scheduling cron jobs that interact with sensitive data, ensure they comply with data protection standards and industry-specific regulations.
  • Optimize Scheduled Tasks. Active cron jobs consume system resources. If a job is no longer necessary or can be run less frequently, review the cron expression and adjust it accordingly.

Conclusion

This guide explained basic cron expression syntax and showed how to use expressions to schedule cron jobs on your Linux, Windows, or MacOS systems.

Next, learn how to schedule Cron jobs on reboot.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Use wget Command With Examples
February 14, 2024

Wget is a commonly used tool for downloading files in a command-line session. Not only is it fast and practical, but it also offers additional downloading features.
Read more
Linux Commands Cheat Sheet
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or download our Linux Commands Cheat Sheet and save it for future reference.
Read more
How to View & Read Linux Log Files
February 13, 2019

All Linux systems create and store information log files for boot processes, applications, and other events. These files can be a helpful resource for troubleshooting system issues.
Read more
How to Kill a Process in Linux
December 13, 2023

If a Linux process becomes unresponsive or is consuming too many resources, you may need to kill it. This complete guide shows how to kill a Linux process using the command line.
Read more