Tuesday, 9 August 2011

Crontab,Its Usage and Syntax

cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris.  Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times. It help us to schedule a job under a specific user to do operations automatically,

1. Crontab Commands
 NB: Make sure that you are switched to the specific cron id(user id) before execute all these.
crontab -e      Edit your crontab file, or create a new crontab under specific user
crontab -l      To see crontab
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file.
crontab /dev/null   To nullify a crontab
crontab -u <user>   Used in conjunction with other options, this option allows you to modify or  view the crontab file of user. When available, only administrators can use this option.(Need to be root)
2.General Syntax of Crontab
*    *    *    *    *  command to be executed
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 7) (Sunday=0 or 7)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)
Eg1a
01,11,21,31,41,51 * * * * /home/wassrvr/a1ibmsvc.gce.sh /home/wassrvr/a1ibmsvc.gce.dat 
This job cron job runs on every 10 minutes,
 Eg1b
Smart people use below one, we can assure both are identical
*/10 * * * */home/wassrvr/a1ibmsvc.gce.sh /home/wassrvr/a1ibmsvc.gce.dat
 This job cron job runs on every 10 minutes,
 Eg2:
* * * * * /web/tools/ihs_logroll.sh > /tmp/ihs_logroll.log 2>&1

They are all still asterisks! So this means

    every minute
    of every hour
    of every day of the month
    of every month
    and every day in the week.

In short: This script is being executed every minute. Without exception.
Eg3:
Cron job  from Tuesday  to Friday at 8 PM 
0 20 * * 2-5 /web/tools/ihs_logroll.sh > /tmp/ihs_logroll.log 2>&1

    minute: 0
    of hour: 20
    of day of month: * (every day of month)
    of month: * (every month)
    and weekday: 2-5 (=Tuesday to Friday)
Mail a cron job
*/10 * * * *  /web/tools/ihs_logroll.sh > /tmp/ihs_logroll.log 2>&1 | mail -s "sub" musthaqpm@yahoo.in

Trashing a crontab or disable mail service
10 * * * * /home/wassrvr/a1wassrv.gce.sh /home/wassrvr/a1wassrv.gce.dat > /dev/null 2>&1
/dev/null can nullify a file,  just pipe the output of the file to /dev/null, which eliminates the  content

How to disable a particular task on a crontab
Just comment the line with  “#” , which disable the task
#31 10 23 * * /system/web/security/scripts/wasHealthCheck.ksh 
In various environment,  If we do changes, cron jobs could be disabled,  If we have  lot of task to be uncomment is bit difficult task, There we used to remove the crontab after taking the backup of the crontab, Once change has implemented we enable the backup file of the cron.
Backup of cron:
wassrvr@g01cdwps003:~> crontab -l > /tmp/crontab.musthaq.bkp   #Backup 
wassrvr@g01cdwps003:~> ls -lrt /tmp/crontab.musthaq.bkp
-rw------- 1 wassrvr websrvr 427 2011-08-13 20:29 /tmp/crontab.musthaq.bkp
wassrvr@g01cdwps003:~>crontab -r     #remove crontab
Enable the cron 
wassrvr@g01cdwps003:~>crontab /tmp/crontab.musthaq.bkp    #load the crontab again 

Cron permissions

The following two files play an important role:
§  /etc/cron.allow - If this file exists, then you must be listed therein (your username must be listed) in order to be allowed to use cron jobs.
Noramlly, it could be removed from the server, since its difficult to enter the name of the people, who really want to use crontab, its tough for root to monitor this, Most of the environment we cant see cron.allow  file
§  /etc/cron.deny - If the cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny file in order to use cron jobs.
musthaq@g01zexwas002:/> ls -lrt /etc/cron.deny
-rw------- 1 root root 11 2010-02-19 11:49 /etc/cron.deny
musthaq@g01zexwas002:/>  sudo cat /etc/cron.deny
musthaq's password:
guest
gast
musthaq@g01zexwas002:/>


No comments:

Post a Comment