Learning Cron by Example
Last updated: Mar 11, 2008If you are using a Linux system and want to schedule a task to run in the future you will probably need to know cron. Cron is much like Window’s Scheduled Tasks. The only difference is that cron is conifgured by a simple text file. Although, that text file to the untrained looks very complicated. Many people rely cron task generators to do the work but I hope that after this guide you will be able to make your own tasks without the use of a generator.
Now obvisouly cron is very dependent and sensitive to the time. If you want accurate results from cron you are going to want to setup your computer to sync its clock via NTP. For now if you don’t have that configured you can use this command to get up to date temporarily:
As root:
ntpdate pool.ntp.org
Editing Cron
There are more than one way to edit the cron config files; however many of them require you to restart the service. We don’t want to do that so here is a method to add a task to cron without having to restart the deamon. You will need to login to the user you wish to execute the command and type:
crontab -e
Here is the basic structure for cron.
m h dom mon dow command
- m - Minutes
- h - Hours (24 time)
- dom - Day of the Month
- mon - Month
- dow - Day of the week
- command - The command you want to run. This can contain spaces or point to a bash script.
Examples
Since many of you like to learn by example, I am going to give you pretty much every example I can think of. This should help you get going with cron.
*’s represent wildcards or any. Under dow 0 and 7 are both Sunday.
- 10 * * * * echo “This command is run at 10 min past every hour”
- 22 7 * * * echo “This command is run daily at 7:22 am”
- 22 20 * * * echo “This command is run daily at 8:22 pm”
- 00 4 * * 0 echo “This command is run at 4 am every Sunday”
- * 4 * * Sun echo “This is the same as the command above”
- 42 4 1 * * echo “This command is run 4:42 am every 1st of the month”
- 01 * 19 07 * echo “This command is run hourly on the 19th of July”
Using the ‘-’ allows us to specify ranges of days. I actually used the following entry recently to run a script at the end of the working day. Execute at 5pm only on weekdays:
* 17 * * 1-5 /path/to/your/code
Using the ‘,’ allows us to specify intervals wihout having to have multiple entries in cron. This would execute the ask on the 1st, the 10th, the 20th and on the 30th of each month, at 17:59PM.
59 17 1,10,20,30 * * /home/username/backupsite
Using the ‘/’ allows us to divide the day into chunks. Here, the tasks is executed every 4 hours (24⁄6 =4).
59 */6 * * * /home/username/backupsite
Here is another example of using chunks of time and a range. Every 20 mins between the hours or 9am and 5pm
*/20 9-17 * * * /path/to/your/code
Dealing with the Output
Sometimes when we execute a command with cron we want to see the results of our command. Other times we could care less. By default the output from cron gets mailed to the owner of the process, or the person specified in the MAILTO variable. Here are some ways we can change the handling of it.
Structure:
cmd | argument
Example of an email output. This will email the results of my command every 4 hours to user, ‘mark’.
59 */6 * * * /home/username/backupsite | mail -s "Subject of Mail" mark
If we don’t care about the output we can send it to nowhere (literally). For example purposes I will just display the cmd and not the entire cron entry.
cmd | /dev/null
Output to a text file and append it if it exists already.
cmd >> log.file
More information can be found in the man page of cron. For more information just type:
man cron
Cron Monitoring for Small Businesses that like to Self-Host
If you run a small business and want to centralize your logging and monitoring. I created a centralized logging system that is easy to self-host called Central Logging. Checkout the cron monitor docs.
Need to print shipping labels on your site?
Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.