Tuesday 31 January 2012

Cron tab Help

The basic usage of cron is to execute a job in a specific time. Please note that the time field uses 24 hours format.

Linux Crontab Format 

min    hr    dom   mon   dow   cmd

min        Minutefield        0 to 59
hr          Hourfield           0 to 23
dom      DayofMonth        1-31
mon       Monthfield         1-12
dow       DayOfWeek        0-6
cmd       Command Any command to be executed.

* Scheduling a Job For a Specific Time Every Day

 30 08 10 06 *  /home/full-backup

* 30 – 30th Minute
* 08 – 08 AM 
* 10 – 10th Day
* 06 – 6th Month (June)
* * – Every day of the week

* Schedule a Job For More Than once a day
take abackup twice a day every day

00 11,16 * * * /home/backup

* 00 – 0th Minute (Top of the hour)
* 11,16 – 11 AM and 4 PM
* * – Every day 
* * – Every month
* * – Every day of the week

* Schedule a Job for Specific Range of Time
If you wanted a job to be scheduled for every hour with in a specific range of time then use the following. Cron Job everyday during working hours.
  during the working hours 9 a.m – 6 p.m

00 09-18 * * * /home/bkp

* 00 – 0th Minute (Top of the hour) 
* 09-18 – 9 am, 10 am,11 am,12 am,1pm,2pm, 3 pm, 4 pm,5 pm,6 pm
* * – Every day 
* * – Every month
* * – Every day of the week

* schedule a job for every minute using cron

* * * * * CMD

The * means all the possible unit — i.e every minute of every hour through out the year. More than using this * directly, you will find it very useful in the following cases.

When you specify */5 in minute field means every 5 minutes

When you specify 0-10/2 in minute field mean every 2 minutes in the first 10 minute

Thus the above convention can be used for all the other 4 fields.

* scheduling a background cron job every 10 minutes

*/10 * * * * /home/cmd

It executes the specified command every 10 minutes through out the year

* Scheduling a cron job in every 6 hours
 0 */6 * * * /path/to/mycommand
It executes the specified command in every 6 Hr throughout the year

Instead of specifying values in the 5 fields, we can specify it using a single keyword. Instead of the above 5 fields you can use @ followed by a keyword — such as reboot, midnight, yearly, hourly.

Cron special keywords and its meaning :-

@yearly  0 0 1 1 *
@daily    0 0 * * *
@hourly  0 * * * *
@reboot Run at startup

* Schedule a Job For First Minute of Every Year using @yearly
If you want a job to be executed on the first minute of every year, then you can use the @yearly cron keyword as shown below. 

@yearly /home/annual-maintenance 

This will execute the shell script at 00:00 on Jan 1st for every year.

 * Schedule a Cron Job Beginning of Every Month using @monthly
This will execute the shell script tape-backup at 00:00 on 1st of every month

@monthly /home/backup

* Schedule a Background Job Every Day using @daily
It will execute at 00:00 on every day

@daily /home/cleanuplogs "day started"

* Execute a Linux Command After Every Reboot using @reboot
Using the @reboot cron keyword, this will execute the specified command once after the machine got booted every time.

@reboot CMD

* Disable/Redirect the Crontab Mail Output using MAIL keyword
By default crontab sends the job output to the user who scheduled the job. If you want to redirect the output to a specific user, add or update the MAIL variable in the crontab as shown below. 

# crontab -l
MAIL="ctechz" 
@yearly /home/maintenance 
*/10 * * * * /home/diskspace

crontab of the current logged in user with MAIL variable. If you wanted the mail not to be sent to anywhere.

MAIL=""

* Execute a Linux Cron Jobs Every Second Using Crontab

We cannot schedule a every-second cronjob. Because in cron the minimum unit you can specify is minute.

* Putting PATH Variable in the Crontab 

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/ctechz

@yearly annual-maintenance
*/10 * * * * check-disk-space

* Installing Crontab From a Cron File
Instead of directly editing the crontab file, you can add all the entries to a file cron-file.txt, then you can upload or install them to the cron as shown below.
make sure you backed up  the entries in crontab first or else the new entries in cron-file.txt will replace the existing entries.

# crontab -l
no cron file defined

# cat cron-file.txt
@yearly /home/maintenance
*/10 * * * * /home/diskspace

# crontab cron-file.txt
execute the cron-file using ctrontab

Then check the crontab file
# crontab -l
@yearly /home/ramesh/annual-maintenance
*/10 * * * * /home/ramesh/check-disk-space 

CronJob for PHP Files

Command to run a PHP5 cron job:
php /home/username/public_html/cron.php

Optional flags are sometimes required for a PHP cron job:
php -q /home/username/public_html/cron.php

Command to use a specific php.ini file:
php -c /home/username/public_html/php.ini /home/username/public_html/myscript.php


Command to GET a remote file:
/usr/bin/GET http://www.example.com/file.php

CronJob for Perl Files

Command to run a CGI cron job:
perl /home/username/public_html/cgi-bin/file.pl

CronJob for Shell Script

Command to run a shell script cron job:
/bin/sh /home/username/public_html/file.sh

No comments:

Post a Comment