Search Site

Feed Section

Friday, March 4, 2011



Get Cracking with Cron

Posted: 03 Mar 2011 09:12 PM PST

Cron is an age-old UNIX program that allows you to schedule your tasks. The command line application that ships with just about every flavor of Linux can be used to schedule things such as backups, syncing your system clock, and other tasks. If you are a system administrator or maintain a Linux server, cron can be your new best friend. The great thing about it is that it is both flexible yet quite easy to setup and use. Let's take a look at what's under the hood of this wonderful tool and how you can use it to make your life easier.

The cron application consists of a few files. There's the binary, which usually sits in your default system path. Then there are a few configuration files such as /etc/contab where you can configure your tasks, /usr/lib/cron/cron.allow and /usr/lib/cron/cron.deny where you can assign which items can access cron and which cannot. For most part, you can just stick to using the /etc/crontab file to schedule tasks. There are also some folders under the /etc directory called cron.hourly, cron.daily, cron.weekly and cron.monthly where you can place your executable scripts to have them run at the said times.

Before you begin using cron make sure that the cron daemon is running. Execute the command "# ps -ef | grep cron" and check to see if you get something in the output that looks like "username 2179 2153 0 15:56 pts/0 00:00:00 grep --color=auto cron". If not, you can start it by executing the command "# sudo service cron restart". On most systems though, cron is started by default, so unless you have disabled it for some reason it should be already running.

There are a number of ways to schedule your tasks using cron -three ways to be precise. You can either make an entry into the /etc/crontab file, drop an executable script into the cron.hourly, cron.daily, cron.weekly and cron.monthly folders, or use the cron command to add your task. There is a slight difference between the three. We'll get to that shortly.

Let's look at each of these methods. The simplest way to add a task to cron is to create a shell script, make it executable using the command "# chmod +x script.sh" and drop it into one of the cron folders under the /etc directory. So, if you drop your backup script into the cron.hourly folder, it will be executed at the stoke of the hour every hour. The same goes for the cron.daily. Your script will be executed at midnight. The limitation with this approach is that it does not offer users the flexibility to execute a script 15 minutes into the hour.

A better approach is to made an addition to the /etc/crontab file. This offers you a great deal of flexibility in terms of scheduling, but it comes at a cost - it is a bit more complicated. An example of a crontab entry is "30 3 * * fri /opt/script/backup_webserver.sh". The format for a cron entry is "minute hour day month dayofweek command". So, in plain English this entry would execute the script /opt/script/backup_webserver.sh at three thirty in the morning of every Friday.

The entries in cron are very flexible in accepting dates. You can enter numerical values, the name of days, and many more things. Use the asterisk sign if you want to it to be executed on every cycle of that particular column. Here's a more detailed list of what goes where:

minute - any number from 0 to 59.
hour - any number from 0 to 23.
day - any number from 1 to 31 (It's a good idea to keep it less than 28 to make sure you cover all months of the calendar).
month - any number from 1 to 12 (or the short name of the month such as jan and feb).
dayofweek - any number from 0 to 7, where 0 or 7 represents Sunday (or the short name of the week such as sun or mon).

The third method you can use to make a cron entry is to use the command "# crontab -e". This opens up an editor-like interface where you can make your entry. It should usually use your default text editor to allow you to make your entries. The format is similar to the one in the /etc/crontab entry.

There you go. That was three ways to use cron to schedule your tasks. Just make sure that you make the cron entries carefully, as getting it wrong could sometimes have bad results, such as a disk full of backups, or a system that hangs or freezes because of too much load.

No comments:

Post a Comment