Monday, August 24, 2009

How to schedule Task in Unix?

We can schedule task in unix based machine using command crontab(symantic meaning: time table)
# Command to see what what crontab currently runing at ur machine
crontab -l

# Command to edit list of running crons
crontab -e
formatt of crontabs would be like below

* * * * *  /usr/myscript/rundummyscript.sh

above five stars stands for
   1. minute (from 0 to 59)
   2. hour (from 0 to 23)
   3. day of month (from 1 to 31)
   4. month (from 1 to 12)
   5. day of week (from 0 to 6) (0=Sunday)

some examples of scheduling jobs
1.Run script every minutes.

* * * * *  /usr/myscript/rundummyscript.sh

2. Run script at  8:15 AM   on every thursday(4)

15  8  *  *  4  /usr/myscript/rundummyscript.sh

3.  Run script at  8:15 AM   on weekdays(1-5)

15  8  *  *  1-5  /usr/myscript/rundummyscript.sh

4. Run script after every past 15 minutes in Jaunary  first day

15  *  *  1  1 /usr/myscript/rundummyscript.sh

5. Run script after every 15 minutes with every 2nd hour means run it as  00 AM (00,15,30,45 min ) and then 02 AM (00,15,30,45 min) and so on.

0,15,30,45  */2  *  *  * /usr/myscript/rundummyscript.sh
                       or
*/15  */2  *  *  * /usr/myscript/rundummyscript.sh
some special kewords

@reboot     Run once, at startup
@yearly     Run once  a year     "0 0 1 1 *"
@annually   (same as  @yearly)
@monthly    Run once  a month    "0 0 1 * *"
@weekly     Run once  a week     "0 0 * * 0"
@daily      Run once  a day      "0 0 * * *"
@midnight   (same as  @daily)
@hourly     Run once  an hour    "0 * * * *
 e.g
@hourly /usr/myscript/rundummyscript.sh

#How to store crontab O/P?
 * * * * *  /usr/myscript/rundummyscript.sh  2>&1 >> /usr/mylog/crontab.log
2 stands for STDERR and 1 for STDOUT. Above statement tells Unix to store
STDERR in STDOUT stream as well.
How to mail cron job O/P?
For this you need to install  "mailx "
and cron job will be like below
* * * * *  /usr/myscript/rundummyscript.sh  2>&1 | mail -5 "cron O/P" urname@urdomain.com

No comments:

Post a Comment