I seem to preach a lot about automation for productivity, and with good reason. You should not have to perform mundane tasks repeatedly. Crontab is a fantastic tool for simply running exactly what you want at times you specify.
Fire up your terminal or Cygwin now.
Suppose I want to copy my personal wiki to my website every other hour between 8:30 and 18:30 on weekdays only. This only takes a couple minutes to setup with a bit of cron-fu.
I’m going to go ahead and use FTP to put my wiki where I want, so I wrote a quick bash script (backup_wiki.sh) for this purpose:
Sweet, so now we can just use backup_wiki.sh
Let’s edit (or create) our new crontab file:
This brings up vi (by default) with a file that may have a comment or may be empty. I don’t feel like using vi right now, so I’ll change it to jEdit by adding the following to my .bashrc file:
export EDITOR="[/path/to/jedit.bat (windows) or 'jedit' (*nix)]"
Ah, that’s better. Now that we can open it up in our fav. text editor, let’s learn how to create an entry in our crontab file.
crontab file structure
Lets break down a sample command that we’ll be putting into our crontab file
This command will run our backup_wiki.sh script at 8:30, 10:30, … 18:30 every Monday(1) through Friday(5). The crontab file basically has one command per line in the following format with the following separated by a space:
- Minutes [0-59]
- Hour [0-23]
- Day of Month [1-31]
- Month [1-12] - January is 1, obviously
- Day of Week [0-6] - Sunday is 0
- Command to run (can have spaces)
An asterisk (*) means all possible values, so in our example above we mean all days of all months. You can specify a range by using a dash (-). Also, using a slash and then a number (like /2) after a command means only run on increments divisible by 2, so 8-19/2 means 8, 10, 12, 14, 16, 18.
Now suppose we just want to
example crontab entries
You’d be surprised what you can automate. Here are some simple examples to give you ideas:
Mark Sanborn has another post about cron that has other useful bits I haven’t covered here.
I hope you found this introduction to cron useful. Please share your ideas for automating with it in the comments!