Sunday, May 13, 2007

Scheduling Tasks in Linux - cron

On Linux systems, you use the crontab tool to schedule tasks that you want carried out on a certain time. Scheduled tasks are called cron jobs. The Linux Task Scheduler (read cron) has several parts to it:

1. The crond Daemon
crond is the workhorse that carries out the real job: executing specified tasks at the specified time. It's a daemon that runs calmly in the background and checks crontab files once every minute and executes tasks whose scheduled time matches the current time.

2. The crontab Tool
crontab is the tool that you use for editing your crontab file.

3. The crontab File
crontab files contain scheduling information of tasks.

The following sections examine each component of the cron system in detail.

The crond Daemon
The crond daemon is responsible for executing tasks at their scheduled time. It gets started automatically by rc scripts at system startup. So, you don't need to mess with it. But there might be times when you want to interact directly with the daemon. Here are a few useful commands to interact with the crond daemon or any other daemon for that matter:

To query status of crond:
# service crond status

To stop crond:
# service crond stop

To start crond:
# service crond start

To stop and start crond in one fell swoop:
# service crond restart

Please note that the service command resides in /sbin directory. On some Linux distros, notably Fedora Core, RHEL, and CentOS, this directory is not referenced in the PATH environment variables. To work around this, you need either to modify the PATH variable in your ~/.bash_profile file to reference /sbin or you need to specify the full path to the service command, i.e., /sbin/service.

The crond daemon executes cron jobs using the /bin/sh shell. Therefore, any environment variables that are available in /bin/sh shell are also available to cron jobs when they are executing.


The crontab Tool

crontab files are not meant to be edited directly. They are edited using the crontab tool. An important thing to note is that crontab files are created on a per-user basis. Whether or not a user can create crontab files is determined by cron.allow and cron.deny files in the /etc directory. Here's how the crontab tool determines permissions:

  • If neither cron.allow nor cron.deny files exist, only the root user can create crontab files.

  • If only cron.allow exists, the crontab tool allows a user to create crontab files if and only if her name is listed explicitly in the file. Other users are denied to create crontab files.

  • If only cron.deny exists, the crontab tool doesn't allow a user to create crontab files if her name is listed explicitly in the file. Other users are allowed to create crontab files.

  • If both cron.allow and cron.deny exist, the crontab tool doesn't allow a user
    to create crontab files unless her name is explicitly listed in the cron.allow file. Other users are denied to create crontab file irrespective of whether their name is listed in the cron.deny file or not.

By default, the crontab tool executes for the user who is issuing the command. If you want to execute a command for a particular user, you'll need to specify the -u option followed by the user name whose crontab file you want to manipulate.

To edit your crontab file:
$ crontab -e

It will open your crontab file in an editor (usually vi) specified by the VISUAL or EDITOR environment variables. You'll need to edit the crontab file and save it. The crontab tool will take care of placing the file in an appropriate location.

To list contents of your crontab file:
$ crontab -l

To remove your crontab file:
$ crontab -r

If you need to list contents of the crontab file of a particular user, say jdoe, you'll execute the following command:
# crontab -u jdoe -l


The crontab file
Here is the format of the crontab file:
Minute Hour Day Month Day-Of-Week Command-To-Execute

Please note that you specify time in military format. Day-Of-Week runs from 0 through 6 representing Sunday through Saturday. Here are some examples of cron jobs:

If you want to shutdown your system at 23:59 every day except Saturday and Sunday, here's how you do it:
# 59 23 * * 1,2,3,4,5 shutdown "now"

Please note that if you want to specify multiple values for a given parameter, you separate them using commas (,) just as I did to specify Monday through Friday using 1,2,3,4,5.

Here's a cron job that executes every 5 minutes and appends current time to a file in the home directory of user jdoe:

# */5 * * * * echo "cron job executed at " $(date) >> /home/ejan/cron.out

Please note how we specified */5 to specify "every five minutes" rather than entering 0,5,10,15,...,55. If you want to execute your cron job at a regular interval like every five minutes, you can simply specify it using */5.


I hope his article will get you up to speed with scheduling tasks in Linux.

1 comment:

Anonymous said...

Best short tutorial on using cron I've seen!