CRON Guide

A guide for everyone

Cron is a tool in Linux that allows you to automate virtually any task you can think of. Basically, cron is able to execute scripts and programs at certain time intervals. The purpose of this guide is to teach you the basics of cron. Even though cron might not be used by everybody on a daily basis, this article still should be useful because when that day comes, you'll know what to do.

There are various ways to work with the cron daemon. Let's take a look in the /etc directory. There, you'll most likely notice several directories called "cron.hourly", "cron.daily", "cron.weekly", "cron.monthly". Even though the name is pretty suggestive, I never imagined that if I place a script in one of those directories, it will get executed at the interval specified by the name of the directory.

We'll actually work a little with the crontab file to instruct cron to perform the various tasks because it makes cron a lot more flexible. The system crontab file is located in the /etc directory, but every user on that machine can have it's own crontab file that is stored in /var/spool/cron/ and which is named the same as the user.

Let's start by looking at the /etc/crontab file.

In the first part of the file we have the variables for cron. Usually, you will not want to modify those and it's not really necessary to have them there because, if they are not specified, cron uses some defaults. The default entry for SHELL is the same as in /etc/passwd file and this also applies for the HOME variable. The PATH variable tells cron where the executable files are. If you have an executable file that is not in the path specified there, you should use the full path when you want to execute it in cron. The MAILTO variable tells cron to mail the output of each command to the user specified there. If no user is specified, the mail will go to the owner of the process.

In the second part, there are four entries that allow us to use the folders we've talked about earlier. These are actually almost normal crontab entries and they can be used as examples. Your entries should be put after these lines.

In each entry there are normally seven fields, as follows:

minute hour dom month dow user command

minute field tells cron what minute of the hour the command will run on. Here, you should specify a value between 0 and 59.

hour supports the 24 clock and the values here should be between 0 and 23.

dom stands for Day Of Month and is the field where you specify what day of the month should a command be executed.

month allows you to tell cron what month a command will be run on. This field support values from 1 to 12 and also the name of the month specified as a three letter abbreviation (e.g. Jan).

dow stands for Day Of Week and accepts values from 0 to 7 and also the name of the day specified in the three letter abbreviation (e.g. Mon).

user is the field where you tell cron as what user to run a command.

command is the last field and here you put in the command that you want to run. In this field, you can use spaces and characters the same way you use them in the shell.

Some simple entries would look like this:

45 9 * * * root echo "Work starts at nine and it's already 9:45 and you're still reading the news :)"

30 * * * 6,7 root echo "It's weekend! Stay away from the computer and enjoy live!"

The lines are self explanatory but I would like to point out some things. In the second line, the command is executed at the middle of every hour each Saturday and Sunday. As you can see here, cron supports comma separated values for its fields. Those are actually called lists and for values like 1,2,3,4,5, you can also use 1-5 and it has the same effect. I actually realized that the first line should be like this:

45 9 * * 1-5 root echo "Work starts at nine! It's already 9:45 and you're still reading the news :)"

Step values can be used in cron either in an asterisk or in conjunction with ranges. For example using 0-23/2 in the hour field executes a command every two hours. It's like putting there 0,2,4,6,8,10,12,14,16,18,20,22. This would have exactly the same effect as */2.

If Microsoft used cron, I think a cron job would be something like this:

*/10 7-20 * * * root echo "WORK! WORK! WORK! Linux will bring us down!"

This would run the command every 10 minutes between 7am and 8pm.

For editing a crontab of a user, you have to log in as that user and enter the command crontab -e. It will open its crontab in vi and you can start editing it. Since vi can't be used by complete newbies, I'll try writing a tutorial for it in the following days. For using another editor, you should run the following command: export VISUAL="editor", where you replace editor with your favorite text editor. I like mcedit the best but it can't always be used because, on most Linux systems, it is not installed by default.

Hopefully, you understood the basics of configuring cron. I strongly advise you to experiment a little with cron if you want to consolidate some of the things you read here.


Photo Gallery (2 Images)

Gallery Image
Gallery Image

Hot right now  ·  Latest news