Linux Unix help !!

"Give respect to Time, One day at right Time, Time will respect You"

Sunday, November 28, 2010

How to schedule cron job Linux

Schedule Crontab jobs

Crontab
It's a unix solaris utility that tasks to be run automatically in background at regular interval.
Daemon: crond

Accessibality:
/usr/lib/cron/cron.allow -> Use can use if your name exist in this file
In absence of this file it's necessary that your name does not exist in
/usr/lib/cron/cron.deny.
And If only cron.deny exist but empty then all users can use crontab.
But if neither exists only root can use crontab.

Commnad:
crontab -l ->List crontab file
crontab -e ->Edit crontab file
crontab -r ->Remove crontab file

====Crontab file fields

* * * * * 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)

Example:
0> Edit crontab for root 
# Login as root
# crontab -e


1>Edit crontab for user shukla
# crontab -u shukla -e

2>Display crontab defined by user shukla
# crontab -u shukla -l

3>Delete shukla's tmp file /home/shukla/dumps/*.* every day at 3:30pm
# crontab -u shukla -e
30 15 * * * * rm /home/shukla/dumps/*

4>Do above every monday-wednesday at 3pm but only in mnth of august
# crontab -u shukla -e
* 15 * * 8 1-3 rm /home/shukla/dumps/*

5>Do above on 1,23 of aug
* 15 1,23 * 8 1-3 rm /home/shukla/dumps/*

6>Do Above at 10:10AM and 6:20pm everyday...
10,20 10,18 * * * rm /home/shukla/dumps/*

7> Sample tricky examples
>Run Every hour
0 * * * * ....
>Run every min
* * * * * * ....

 >Run every 5 min
*/5 * * * *  .....

>Run ever 2 hour interval eg:00:05, 02:05
5 */2 * * *

>Ever month except april
* * * jan-mar,5-12 * * OR
* * * 1-3,5-12 * *

>On every reboot display msg alert
@reboot wall "PC is going to REBOOT"

>Ever hour same above --->this will run 1st sec of every hr equivalent to (0 * * * *)
@hourly wall "PC is going to REBOOT"

Some similar too:
@yearly --> (0 0 1 1 *)
@annually --> (0 0 1 1 *)
@monthly --> (0 0 1 * *)
@weekly --> (0 0 * * 0)
@daily --> (0 0 * * *)
@midnight --> (0 0 * * *)

--Shirish Shukla

Followers

Pls LIKE my Story !!!