Scheduling Tasks in CentOS – crontab

Crontab is the venerable task scheduler for Unix-derived systems. To schedule tasks, you configure a table of entries that occur in some sort of chronological fashion, hence “crontab”.

The 'man' command can display a manual page fo...
Image via Wikipedia

To set up a crontab entry, enter crontab -e at the shell prompt. This will bring up an entirely blank screen in vim, the default editor for CentOS. Pretty intimidating, huh? Exit out of Vim (hit ESC, then hit ‘ZZ‘ to exit or hit the Escape key and type :q and hit the Enter key)  and try again. You can start by visiting the manaul page for the crontab table layout: man 5 crontab at the shell prompt. This includes a helpful table of what each of the values in the crontab table means. I always copy it into my crontab, as I can never remember the values. Here’s what to insert into the blank crontab:

#       The time and date fields are:
#
#              field          allowed values
#              -----          --------------
#              minute         0-59
#              hour           0-23
#              day of month   1-31
#              month          1-12 (or names, see below)
#              day of week    0-7 (0 or 7 is Sun, or use names)
# Turn off mail
MAILTO=""

Each line is prefixed with the hash mark comment character, with the exception of the last line, which we’ll get to in a minute. So, try again: crontab -e, put yourself into insert mode in Vim (hit the letter ‘i’) and paste in the above section. Hit enter a few more times to create some blank spaces. Now hit the Escape key to leave insert mode and hit ‘ZZ’, which should cause the editor to edit. You should see a message about installing the new crontab. If you see an error message, edit the crontab file again until it installs with no errors.

At this point, you have a functioning but empty crontab. Now you can return to editing the file to actually add something that does something for you on a scheduled basis. For example, the following line will run a command at 5 minute intervals every day:

# Run every five minutes
*/5 * * * * (cd /home/bjchapm/r2e;/home/bjchapm/r2e/r2e run)

You will also receive an email from cron every five minutes, unless you have entered the magic MAILTO=”” in your crontab. Generally, you’ll want to stop the sending of mail once you’ve tested and confirmed that your script works properly.

For more information about exactly what can be entered in each of the crontab fields, just revisit the manual page for crontab: man 5 crontab. Incidentally, the “5” refers to section 5 of the manual. If you type man crontab instead, you will not get anything useful – make sure you’re typing man 5 crontab. Just a little Unix obscurity to spice up your day.

Changing your editor

Some of you (many of you? all of you?) may want to use another editor rather than Vim to edit your crontab files. You can try this at the command prompt to make another editor the default:

export VISUAL=nano

This will set your “visual” editor to nano. Nano is installed by default on my CentOS installs. It’s somewhat reminiscent of the old Wordstar-style editors. It uses control key+letter combinations to execute commands. If it’s not installed on your version of CentOS, try (as root) yum install nano. Once nano is installed and you set and export the VISUAL variable as above, typing crontab -e will take you into a nano session.

Reblog this post [with Zemanta]
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s