Monday, December 26, 2011

Job Schedule (crontab)

CRONTAB(Task Scheduling) 
Cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon.

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

Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.
B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .
 
Question 1:Add a cronjob to display "HELLO WORLD"  on every 2 minutes in terminal(tty)2
 [root@localhost ~]# vim /schedule.txt
*/1 * * * * /bin/echo "HELLO WORLD" > /dev/pts/2
[root@localhost ~]# crontab /schedule.txt
[root@localhost ~]# service crond restart
Stopping crond:                                            [  OK  ]
Starting crond:                                            [  OK  ]
Press tty for check which terminal we are currently working 
[root@localhost ~]# crontab -e for show all active schedule

 
Question 2:The user robert must configure a crontab that run daily(31st december) at 15:25
local time and execute /bin/echo i got RHCE certificate 
 
[root@localhost ~]# useradd robert
[root@localhost ~]# passwd robert
Changing password for user robert.
New UNIX password:
BAD PASSWORD: it is too simplistic/systematic
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]# crontab -e -u robert
25 15 31 12 * /bin/echo "I GOT RHCE CERTIFICATE"
wq
crontab: installing new crontab
[root@localhost /]# crontab -l -u robert
25 15 31 12 * /bin/echo "I GOT RHCE CERTIFICATE" 
For remove crontab for user robert
[root@localhost /]# crontab -r -u robert
[root@localhost /]# crontab -l -u robert
no crontab for robert
===================================================
[root@localhost named]/usr/bin/free
[root@localhost named]minute-hour-date-month-date of week
[root@localhost named]0-59/8-17/1-31/1-12/ 0/7=sun,1=monday 
[root@localhost named]crontab  -e
*/10  8-17  *  *  *  /usr/bin/free;  /bin/ps | mail  -s memoinfo  root@host
/usr/bin/free  > filename 
[root@localhost named]mail      q  to sign out
[root@localhost named]mutt  to check mail
[root@localhost named]crontab  -l -u  username  ( to see jobs of another users)
[root@localhost named]crontab  -l  ( to see log in users jobs)
[root@localhost named]crontab -e
* *  *  * *  /home/username/jobname  (to schedule jobs of another one users jobs) 
[root@localhost named]crontab  -r  ( to remove log in users jobs)
[root@localhost named]crontab  -r  -u username (to remove jobs of another one users jobs)