Schedule Scripts Using Crontab
Many Unix systems come preinstalled with crontab
, a tool that generates a file where each row is a cron job, or a program/script set to run at a specific time or interval.
Creating cron jobs is useful for periodically running programs. For example, you could create a script that searches your directory for files older than 1 year, and then create a cron job that runs this script once a month.
To specify the schedule for which a cron job should run, you need to use cron schedule expression. Cron schedule expressions have the following format:
The first place represents the minute that the cron job should run at. The second place the hour, the third place the day of the month, the fourth the month, the fifth the day of the week.
A *
indicates to run at "any" time. So a *
over the month position would mean run on any month.
Prepending a number with */
means "every" . So a */5
over the minute position would mean run every 5th minute, A.K.A. every 5 minutes.
The following expression would dictate that the cron job run at 6:00 AM everyday, A.K.A. the 0th minute of the 6th hour on any day of the month, any month, and any day of the week.
The following expression would dictate that the cron job run every 30 minutes on Saturday, A.K.A. every 30th minute of any hour on any day of the month, any month, and on the 6th day of the week.
It's easy to get confused making cron schedule expressions, so I recommend using a site such as https://crontab.guru/ that makes it easy to construct the expressions and describe them to you.
Once you have a cron schedule expression, you can create a cron job in your crontab using the command crontab -e
, which will open an editor for you the add your cron job. Put your cron job on an empty line with the following format:
Last updated