Cron: Making Computers Work For You

So, you want to make your computer start doing stuff for you with out even having to ask? We all know the myth that computers were created to preform the menial tasks humans would get bored with. The problems is they often *create* more than they solve. How often have you had to do something and though, “If only the computer would just take care of this for me every Sunday.”

There’s an answer, and that answer is called cron. Cron is magical. It allows you to set up a simple text file you can use to tell the computer to do something at some time, or multiple times. The most common uses for cron are administrative but you can really do whatever you want with it.

What is cron? Cron sits around and every minute your computer is running it will wake up, read the crontrab file (more on that later), compare the current time w/ the times listed in said file and then if anything matches preform the task described. There are a couple different versions of cron but the most common is vixie-cron written by Paul Vixie. It is so common pretty much every distro comes with it, so if for some reason you didn’t install it w/ when you were installing your distro you can use whatever package management system you’ve got to install it.

Now you have cron installed and you want to get started. In order to start automating your processes you needed to edit the crontab for the user you want to preform the task. The user whose cron file lists the action is who’s user space the task will be preformed under. Therefor if you need root privs to preform your task you should edit root’s crontab. Just how do you do that? Well that’s simple. You issue the command “crontab -e” (If you’re root or happen to have access to another account you’re not currently logged in under you can edit a specific user’s crontab with the -u username flag).

crontab is exceedingly simple, it is just a wrapper for your default editor. If you don’t want to use your default editor for some reason you can always change it by altering your EDITOR env var according to your shell’s syntax. (BASH for example would use “export EDITOR=vim” to use vim) This is all great and simple, but now you’re probably wondering just what you need to type in there to make it useful.

Cron utilizes 6 separate fields:
1. The minute (0-59)
2. The hour (0-23)
3. The day of the month (1-31)
4. The month (0-12 or July, Dec, etc)
5. The Day of the Week (0-6 where 0=Sunday or Sun, Mon, etc)
6. The actual task you want preformed.

To get you in the swing of things I’ll show you two entries from my own crontrab and then we can talk about what they do and how to change stuff.

0 0 * * *       fortune > /etc/motd
0 4 * * 0       find / -name '*~' -type f -exec rm -f {} \;

The first line changes the message of the day every day at midnight with the fortune program. The second line searches the entire file-system looking for files (-type f) with the a name that ends with a tilde (-name ‘*~’) and forcefully removes them (-exec rm -f {} \;) every Sunday. These files are created by a number of different applications for a back up, but they can also start to take up space and often times just get in the way. One thing to keep in mind when using something like find is that you might want to test what you’re doing somewhere safe first. If you accidentally used rm -rf for example and forgot the -type f param well, there could be problems.

These are just simple examples but already I think you can start to appreciate the power of cron. Anything you can do on the CLI can be turned into a cron job and repeated indefinitely with out you having to worry about it all the time. Perhaps you have a backup bash script you wrote and have to run every Friday after work, you can set up a cron job to run it for you and mail you the report!

This brings up another point, cron will mail you with anything sent to STOUT or STERR using the local mail system. If you expect errors but don’t want to clog up your mail folder with reports from cron about what you already know is going to happen you can append something like 2>&1 > /dev/null

Ok, we have the basics under our belts now but there’s still a little bit more to cover. What if you want to have some script run every 15 minutes? would you need 96 entries? how about just having it run on Mondays and Thursdays? Rest assured there’s no need to create a 100 line crontab. Cron supports stepping, ranges, and lists. Just what are these? Let’s start with the most basic – a list. A list is simply a set separated by commas. For example 2,5,19 placed in the hour field will run the task at 2am, 5am and 7pm. Continuing backwards we have a range which is denoted by a dash. To use a range to make a task run only during the summer you could use Jun-Aug (depending on your definition of “summer”). last we have step which is quite interesting and adds a lot of flexibility and power to cron and is denoted by the backslash. To get our fictions task to run every 15 minutes we would use */15 for the minutes and 0-23 for the hours. Another example would be say, 10-20/2 would run at 10,12,14,16,18 and 20 (it steps ever 2… get it? ;). Any field can also mix and match these options such as “2-4,8-17/3,20” in the hours field.

Once you’ve finished editing your crontab file use whatever method your editor employees for saving and exiting and you should see something like:
crontab: installing new crontab

If crontab notices any errors in your syntax it will also spit our an error like:
crontab: installing new crontab
“/tmp/crontab.XXXXJhoVUm”:1: bad minute
errors in crontab file, can’t install.
Do you want to retry the same edit?
type y and hit enter to try again or n and enter to ignore the error, though I wouldn’t advise doing that.

At this point you should have a firm understanding of just what cron is. You should know the various fields the crontab uses and which values they accept. Most importantly you know how to get into edit your crontab with your favorite editor to set up all your fun (or not so fun) tasks. Now you too can sit back and enjoy… whatever it is you enjoy while the computer automatically churns through all the tedium.

Leave a Reply

You must be logged in to post a comment.