linux server – cron , crontab

Cron

Cron is a daemon that executes scheduled commands. Cron is started automatically from /etc/init.d Cron searches its spool area (/var/spool/cron/crontabs) for crontab files. It load crontab files on memory to run.

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon.

 

We cannot access crontab files directly , it will be accessible via crontab.

Cron also reads /etc/crontab, which is in a slightly different format. Additionally, cron reads the files in /etc/cron.d.

Cron runs every minute and check for whether any job has to run or not. It can modify automatically so we do not nee dto restart server after making changes to crontab.

Crontab

Crontab is the program which contains lists of job to be run. Crom daemon looks that list and run the job as per scheduled.

 

crontab -e    Edit your crontab file, or create one if it doesn’t already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.)

 

Crontab format:-

Commands are executed by cron when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) match the current time.

 

Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

 

A field may be an asterisk (*), which always stands for “first-last”.

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an “hours” entry specifies execution at hours 8, 9, 10 and 11.

if you want to say “every two hours”, just use “*/2”.

example

minutes , hours, day of month , month , day of week, command(or path of shell script)

An example of crontab format with commented fields is as follows:

# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        2          12             *               0,6           /usr/bin/find

reference:

 

http://www.pantz.org/software/cron/croninfo.html

http://www.adminschoice.com/crontab-quick-reference/

example of crontab:-

[anand@server001 ~]$ crontab -l
#*/5 * * * * /home/anand/scripts/apacheMon.sh
*/5 * * * * /home/anand/scripts/mysqldMon.sh
#*/5 * * * * /home/anand/scripts/apacheaccesslog.sh
*/5 * * * * /home/anand/scripts/apacheerrorlog.sh
*/10 * * * * /home/anand/scripts/cpuidle.sh
*/10 * * * * /home/anand/scripts/dbstat.sh
*/5 * * * * /home/anand/scripts/diskalert.sh
00 00 * * * /home/anand/scripts/deadlock.sh

[anand@server001 ~]$ more /home/anand/scripts/deadlock.sh
#!/bin/sh
#***********************************************************************
#Script to check deadlock
#Created by Anand
#***********************************************************************
Maillist=”anand.kumar@test.com”
host=`hostname`
File_Location=”/apps/www/anand/log”
#printf ‘file location’$File_Location
#dateformat=`date +%d-%m-%Y`
dateformat=`date +%d-%m-%Y -d “1 day ago”`
#printf ‘date format’$dateformat
FILE=$File_Location/$dateformat\_DbFailure.txt
if [ -f $FILE ];
then
Count=`grep -i “Invalid query: Deadlock found when trying to get lock;” $FILE | wc -l`
if [ $Count -gt 0 ]
then
echo -e “Invalid query: Deadlock found on Server 001, Please manually run the insert commands.\n Number of deadlock found:$Count.\n Path of insert query is: $FILE” > /tmp/Error
mail -s “Critical!! – Deadlock while insertion in server001” $Maillist < /tmp/Error
fi
fi
[anand@server001 ~]$ vi /home/anand/scripts/deadlock.sh

To edit the shell script.

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

%d bloggers like this: