Before configuring cron jobs, it's essential to grasp the syntax and formatting of cron to ensure
smooth
script execution. The crontab syntax encompasses five fields, each accepting specific
values
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * * [command to execute]
- Minute: Denotes the minute of the hour for command execution, ranging from 0-59.
- Hour : Specifies the hour for command execution in a 24-hour format (0-23).
- Day : Sets the date of the month for command execution, ranging from 1-31.
- Month : Determines the month for command execution, ranging from 1-12/JAN-DEC (representing January to December).
- Day of the week : Sets the day of the week for command execution, ranging from 0-6/SUN-SAT, where 0 represents
Sunday-Saturday, and in some systems, 7 represents Sunday.
Apart from understanding syntax, familiarity with cron job operators is imperative for modifying values in each field:
- Asterisk (*): Represents all possible values in a field. For instance, using * in the Minute field schedules the
cron job to run every minute.
- Comma (,): An operator for listing multiple values. For example, writing 1,5 in the day-of-week field will
schedule the job to run every Monday and Friday.
- Hyphen (-): Specifies a range of values. For instance, using 6-9 in the Month field sets up a cron job from June
to September.
- Separator (/): Divides a value. To run a script every twelve hours, use */12 in the Hour field.
- Last (L): Utilized in the day-of-month and day-of-week fields, where, for example, 3L in the day-of-week field
signifies the last Wednesday of the month.
- Weekday (W): Determines the closest weekday from a given time. For instance, 1W in the day-of-month field will
execute the command on Monday, the 3rd, if the 1st of the month falls on a Saturday.
Special strings are used to quickly schedule cron jobs at certain time intervals without specifying the exact values.
To use them, write a simple phrase starting with an @. Here are some useful special strings to use in commands:
- @hourly: The job will run once an hour.
- @daily / @midnight: These strings will run the task every day at midnight.
- @weekly: A string for scheduling tasks once a week at midnight on Sunday.
- @monthly: This special string runs a command once on the first day of every month.
- @yearly: Use this string to run a task once a year at midnight on January 1st.
In cron syntax, the asterisk (*) means ‘every,’ so the following cron strings are valid:
- Run once an hour at the beginning of the hour:
0 * * * *
- Run once a day at midnight:
0 0 * * *
- Run once a week at midnight on Sunday morning:
0 0 * * 0
- Run once a month at midnight of the first day of the month:
0 0 1 * *
- Run once a month on the 22nd:
0 0 22 * *
- Run once a month on the 2nd Monday:
0 0 * * 1#2
- Run once a year at midnight of 1 January:
0 0 1 1 *
- Run every other Sunday at 0900 hours:
0 9 * * sun/2
- Run twice a month at 3 AM, on the 1st and 15th of the month:
0 3 1,15 * *