Want your script to run at regular intervals? Maybe irregular intervals is what you want? There’s a unix/linux/mac utility you can use called cron. Setup is easy, just type in “crontab -e” to edit your cron jobs.
In the crontab, each job will have it’s own line. Each line starts with 5 special values, separated by at least 1 space, that will determine when the program is run. These values, in order, are:
- Day of the month
- Month of the year
- Day of the week (0=Sunday)
Each of these can have different values:
|1||Runs only when the value is 1|
|30||Runs only when the value is 30|
|1,2,5||Runs only when the value is either 1, 2, or 5|
|1-5,8-10||Runs only when the value is either 1, 2, 3, 4, 5, 8, 9, or 10|
|*/5||Runs only when the value is a multiple of 5|
|*||Runs at any value|
The most used value is the star “*”. This is because most of the time, you will only want 1 or 2 values to be real numbers. Here are some examples:
* * * * * echo 'I run once a minute, every minute, every hour' 0 * * * * echo 'I run at the top of the hour, every hour' 0 12 * * * echo 'I run at 12:00 noon, every day' 25 4 * * 1 echo 'I run at 4:25am, every Monday' 00 22 24 12 * echo 'There is only 2 hours left until Christmas'
As you can see, the combinations are endless. You can call any command you’d like after the 5 crontab fields. I am just calling the echo command, which is pretty pointless because the cron is run in the background, so the output is never rendered to the screen.
Email cron output
If you do want the output to go somewhere, add MAILTO=”email@example.com” to the top of your crontab. If email is all setup on your system, it will email the output of all cron jobs to the email provided at the top of the file.