Cron, sometimes also called (somewhat wrongly) Cron job, is a popular job scheduler for the Unix operating system. You can set up ‘canned jobs’ for executing later at a predetermined time. You can also schedule jobs for executing at a regular interval.
In this guide, we are going to list several useful Cron commands. These commands can be used with minimal to no editing for scheduling your job.
- Cron expression
- Run every minute
- Run every minute only on weekdays
- Run every hour
- Run at 9 AM on the morning on weekends
- Run at 10:30 PM every day
- Run at 10:30 PM on the last Wednesday of every month
- Run at 1 PM, 2 PM, and 3 PM every day
- Run every 4 hours at 15 minutes
- Run at 2:15 AM on the third Friday of every month
Cron expression
A standard Cron expression consists of a series of 5 or 6 fields. Each of these fields is separated by white spaces. The fields are as follows:
* * * * * *
| | | | | |
| | | | | —— The day of the week – allowed values are 0-6 or SUN-SAT
| | | | ——=—– The month – allowed values are 1-12 or JAN-DEC
| | | —————— The day of the month – allowed values are 1-31
| | ———————— The hour of the day – allowed values are 0-23
| —————————— The minute of the hour – allowed values are 0-59
———————————— The second of the minute – allowed values are 0-59
Run every minute
Say, you want to execute a script very often (every minute, for example!). As we are not specifying anything other than the minute, we can use asterisk for all the fields (except the ‘second’ field).
0 * * * * *
| | | | | |
| | | | | —— Run every day of the week (nothing specified)
| | | | ——=—– Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | ———————— Run every hour (nothing specified)
| —————————— Run every minute
———————————— Run at the start of the minute
Run every minute only on weekdays
You can make use of the convenient range-based method for specifying a list of values for some fields. For specifying a sequence of consecutive values, you use them in the following format: {start}-{end}.
0 * * * * 1-5
| | | | | |
| | | | | —— Run every weekday
| | | | ——=—– Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | ———————— Run every hour (nothing specified)
| —————————— Run every minute
———————————— Run at the start of the minute
Run every hour
This can be used for executing tasks that need to be run less frequently.
0 0 * * * *
| | | | | |
| | | | | —— Run every day of the week (nothing specified)
| | | | ——=—– Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | ———————— Run every hour (nothing specified)
| —————————— Run at the start of the hour
———————————— Run at the start of the minute
Run at 9 AM on the morning on weekends
You want to set an alarm at 9 AM on the weekends. Here, you have to specify four fields – the second, minute, hour, and day of week field. We set the second and minute fields to 0, the hour field to 9 (execute it at the beginning of the hour), and the day of week field to either ‘SAT, SUN’ or ‘6,0’ without the quotes.
0 0 9 * * SAT,SUN
or
0 0 9 * * 6,0
| | | | | |
| | | | | —— Run every weekend
| | | | ——=—– Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | ————————
| ——————————
Run at 10:30 PM every day
Running at some specific time every day is one of the most basic use cases of Cron. We need to specify only the minute field and the hour field.
0 30 22 * * ?
| | | | | |
| | | | | —— Can be used instead of an asterisk
| | | | ——=—– Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | ————————
| ——————————
Run at 10:30 PM on the last Wednesday of every month
For executing such a complex command, you can take advantage of one of the special characters that Cron supports. The ‘L’ character can be used to indicate some specific last day of the month. You can use this character in only the day of the month or day of the week field.
0 30 22 * * ?
| | | | | |
| | | | | —— Run every last Wednesday of the month
| | | | ——=—– Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | ————————
| ——————————
Run at 1 PM, 2 PM, and 3 PM every day
This is the perfect example where you need to use a list of values. You can use multiple values for the hour field in the {v1},{v2},…,{vn} format.
0 0 13,14,15 * * *
| | | | | |
| | | | | —— Run every day of the week (nothing specified)
| | | | ———— Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | —————————– Run at 1 PM, 2 PM, and 3 PM
| ————————————- Run at the start of the hour
——————————————- Run at the start of the minute
Run every 4 hours at 15 minutes
Whenever you need to specify something that is periodic, something that needs to be run over and over at certain intervals, use the backslash (‘/’) syntax. Anything after the backslash character is used to denote the interval or increment of range for that field. So, when you use ‘\4’ for the hour field, you specify to run it after every 4 hours.
0 15 /4 * * *
| | | | | |
| | | | | —— Run every day of the week (nothing specified)
| | | | ——=—– Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | ———————— Run every 4 hours
| —————————— Run after 15 minutes
———————————— Run at the start of the minute
Run at 2:15 AM on the third Friday of every month
In case of a condition like this, you need to use the Hash (‘#’) character. This special character is allowed only for the day of week field. The number following the Hash is used to signify the corresponding position of that week of the day. So when you need to specify the ‘third Friday’, you use ‘5#3’ for the week of the day field.
0 15 2 * * 5#3
| | | | | |
| | | | | —— Run every third Friday of the month
| | | | ——=—– Run every month (nothing specified)
| | | —————— Run every day (nothing specified)
| | ————————
| ——————————
———————————— Run at 02:15 AM