Mar 15 2008

Automating jobs with cron

Published by Lord TCT at 9:34 am under Linux

Linux

cron is probably one of the most useful services in the UNIX world. It is a time-based scheduling service that executes commands at a set interval.

Most flavours of Linux should have cron bundled. Unfortunately, not every distribution has the same method of configuration. The following example is based on CentOS 5, a popular enterprise web server distribution based on the Red Hat Enterprise Linux.

The configuration file for cron is stored in /etc/crontab. This file stores a list of commands to execute at a certain time interval you set.

1. Open /etc/crontab with a text editor:

$> vi /etc/crontab

For per-user crons, just enter:

$> crontab -e

2. Enter the command to run and its parameters:

A cron job is a single line that looks like this:

00 00 * * *  webalizer -c /var/logs/webalizer.conf

The blue side is the timer settings:

[minutes] [hours] [days] [month] [weekday]

Minutes: 00-59 – Minute of the hour
Hours: 00-23 – Hour of the day
Days: 01-31 – Day of the calendar month
Month: 01-12 – Month of the year
Weekday: 00-07 – Day of the week

* represents recurring, i.e. * in [hours] means every hour.

The green side is the command to execute, along with its command line arguments.

3. Save the file and restart cron

To save a file in vi, press escape and type :wq

For CentOS 5 restarting cron is as easy as:

$> service crond restart

If you have used crontab to install your crons, no restarting is required!

For the rest, refer to your distribution’s documentation for help.

A kind reader, Pin, has kindly reminded me that I left out the “MAILTO=” parameter on the top of the crontab file. Setting this will make crond send the output of the cronjob to the designated email. On most distrubutions, this is set to “MAILTO=root” on default. but you can change it to anything you like.

Example:

1. To run a job every 2 hours every day, we would write something like:

00 0,2,4,6,8,10,12,14,16,18,20,22 * * * job.sh

2. Every minute (say for MRTG)

* * * * * mrtg -c something.config

3. Every mondays at 6am (say to update race results of the weekend)

00 06 * * 0 php raceupdate.php

4. Once a year on December 25 (Change site theme to Christmas theme)

00 00 25 12 * php themes.php -t christmas.css

5. Backup the database every hour during December 24 (Christmas last minute shopping rush!Backup db!):

00 * 25 12 * mysqldump db-Shop -u root > ecommerce.sql

2 responses so far

2 Responses to “Automating jobs with cron”

  1. Pinon 10 Apr 2008 at 1:40 pm

    I used to have first line in the Crontab as ‘MAILTO=root’. Then I will create a file called .forward in /root and include my email there. When Contrab run into any error, it will trigger the email and send to myself :)

  2. Lord TCTon 10 Apr 2008 at 10:05 pm

    Thank you Pin for the tip! Much appreciated!

Trackback URI | Comments RSS

Leave a Reply

Anti-spam challenge:
Sorry but you forced me to do this!