What is a cron job?
A cron job is a time-based job scheduler in Unix-like operating systems. It is used to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals. i.e., every single hour, every day at midnight, every Sunday at 3:00 AM, or once a year, etc.
Cron jobs are used to automate repetitive tasks like system maintenance, backups, and other system-related tasks. For instance, you can use a cron job to schedule a script that backs up your database daily at midnight.
How cron pattern work?
A cron pattern is a string that represents the schedule of a cron job. It consists of five fields separated by spaces. Each field represents a unit of time (minute, hour, day of month, month, and day of week) and can contain a specific value, a range of values, a list of values, or a step value.
You can watch this video where I explain how a cron pattern works in detail:
Here is the format of a cron pattern with possible values for each field:
* * * * *
| | | | |
| | | | +----- day of the week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- minute (0 - 59)
NOTE: If you use node-cron
in your Node.js application, the cron pattern has an additional field for seconds. It will be like: * * * * * *
. I have shown this on my video tutorial.
Each field in the cron pattern can contain the following values:
-
*
: The asterisk (*) represents all possible values for that field. -
number
: A specific number represents that exact value for that field. -
range
: A range of values separated by a hyphen (-) represents all values within that range. -
list
: A list of values separated by a comma (,) represents all values in the list. -
step
: A step value represents a range of values with a step size. It is specified asstart-end/step
.
You are also allowed to combine these values to create complex cron patterns.
For example, 1 12 4 1-3,5,8 *
represents a cron pattern that runs at 12:01 PM on the 4th of January, March, May, and August.
Here are some useful cron pattern examples:
1. Run every minute
* * * * *
2. Run every 5 minutes
*/5 * * * *
3. Run at midnight every day
0 0 * * *
4. Run at 3 AM every day
0 3 * * *
5. Run at 6:30 PM every day
30 18 * * *
6. Run every Monday at 9 AM
0 9 * * 1
7. Run on the 1st day of every month at midnight
0 0 1 * *
8. Run every day at 11:15 PM
15 23 * * *
9. Run every 15 minutes
*/15 * * * *
10. Run at 5 AM on the first Monday of every month
0 5 1-7 * 1
11. Run at 8 AM every weekday (Monday to Friday)
0 8 * * 1-5
12. Run every Sunday at midnight
0 0 * * 0
13. Run every 10 minutes between 9 AM and 5 PM
*/10 9-17 * * *
14. Run on January 1st at midnight (New Year's Day)
0 0 1 1 *
Use Crontab Guru to generate cron patterns
It is a useful tool to generate cron patterns by providing a human-readable description of the schedule.
Conclusion
That's it! I hope you find this article helpful. If you have any questions or feedback, feel free to leave a comment below.
Top comments (1)
Thanks for the article, good read. Beside Crontab Guru, this similar Cron Expression Generator would be helpful too.