Linux and Open Source articles, topics and discussion with your questions answered by dmourati, a Linux expert with over eight years production experience.

Wednesday, December 12, 2007

Working with Cron

In today's installment dmourati will show you how to setup cron to do your bidding. With a little advanced planning, you can have cron run jobs for you while you sleep freeing you up to do other, more interesting work.
cron

Cron is a daemon to execute scheduled commands. This can be setup on a system-wide basis, or for individual user accounts. Today we will setup a system level cron.

Configuring a Cron Job

To set up a system cron job, make a new file in /etc/cron.d. This directory is used to store cron entries. Here's an example:


[root@lncdw2 cron.d]# cat ds-log-syncer
30 00 * * * root /usr/local/sbin/ds-log-syncer.sh > /dev/null 2>&1


This entry starts off with the standard cron format:

minute hour day month day-of-week user script

A "*" above just means every.

So, this can be read as follows

At 30 minutes past midnight, every day, the root user should run the following script /usr/local/sbin/ds-log-syncer.sh and redirect the output to /dev/null (throw it away).

Script

Here's the shell script that the cron entry refers to:

#!/bin/bash
#Streaming logs:
#From DS:/var/log/streaming/* to
#lncdw2:/var/log/streaming/
#Httpd logs:
#From DS:/var/log/httpd/* to lncdw2:/var/log/httpd/

#Setup directories
mkdir -p /var/log/streaming/Three/threeprodds1/ /var/log/streaming/Three/threeprodds2/ /var/log/streaming/Three/threeprodds3/ /var/log/streaming/Three/threeprodds4/ /var/log/streaming/Three/threeprodds5/
mkdir -p /var/log/httpd/Three/threeprodds1/ /var/log/httpd/Three/threeprodds2/ /var/log/httpd/Three/threeprodds3/ /var/log/httpd/Three/threeprodds4/ /var/log/httpd/Three/threeprodds5/

#Get Three streaming logs
echo "Synchronizing Three streaming logs" | logger -s -t ds-log-syncer.sh
rsync -avze ssh rhythm@threeprodds1:/var/log/streaming/ /var/log/streaming/Three/threeprodds1/&
rsync -avze ssh rhythm@threeprodds2:/var/log/streaming/ /var/log/streaming/Three/threeprodds2/&
rsync -avze ssh rhythm@threeprodds3:/var/log/streaming/ /var/log/streaming/Three/threeprodds3/&
rsync -avze ssh rhythm@threeprodds4:/var/log/streaming/ /var/log/streaming/Three/threeprodds4/&
rsync -avze ssh rhythm@threeprodds5:/var/log/streaming/ /var/log/streaming/Three/threeprodds5/&

#Get Three httpd logs
echo "Synchronizing Three streaming logs" | logger -s -t ds-log-syncer.sh
rsync -avze ssh rhythm@threeprodds1:/var/log/httpd/ /var/log/httpd/Three/threeprodds1/&
rsync -avze ssh rhythm@threeprodds2:/var/log/httpd/ /var/log/httpd/Three/threeprodds2/&
rsync -avze ssh rhythm@threeprodds3:/var/log/httpd/ /var/log/httpd/Three/threeprodds3/&
rsync -avze ssh rhythm@threeprodds4:/var/log/httpd/ /var/log/httpd/Three/threeprodds4/&
rsync -avze ssh rhythm@threeprodds5:/var/log/httpd/ /var/log/httpd/Three/threeprodds5/&



As you can see, this script is used to synchronize log files down to this local system. Some "logger" statements have been added to generate useful log messages on this host as well.

Running It

The cool thing is that once you set this up, there is nothing left to do. Sit back, relax, and enjoy synchronized logs.

For more information on rsync over SSH as used in the script, stick around linfactory.

No comments: