Wednesday, 20 April 2011

Shcedule task using CRON


      Shcedule task using CRON
      1 To add utility
      2 Entry for job schedule
      3 check /etc/passwd
      4 Edit /etc/cron.allow
      5 Service restat

      1 To add utility for loggedin user
        List job schedule                                   #crontab -l
        View/Edit job schedule                              #crontab -e
        remove job schedule                                 #crontab -r
        To add utility for logoff user (ie by root)
        List user specific job scedule                      #crontab -lu username
        View/Edit user specific job schedule                #crontab -eu username
        romove user specitic job schedule                   #crontab -ru username
      2 Entry for job schedule                              #Crontab -e             [for login user]
                                                            or
                                                            #crontab -eu tawheed    [cron job by root]
         schedule: evry 11.50pm of monday of jan            console (vim)
         task:    /bin/echo hello                          50 23 * Jan 1 tawheed /bin/echo hello
         Schedule Fields r sparated by space                Minuite Hour Day_of_month Mont_of_year Day_of_Week
         Multiple task separated by semicolon (;)
      3 check /etc/passwd                                   #cat /etc/passwd              [for user specific job]
      4 Edit /etc/cron.allow                                #echo ALL > /etc/cron.allow
                                                            #echo tawheed > /etc/cron.allow
      5 Service restat                                      #service crond restart
     
N.B.  Multiple value    *           [all]
                        12,15,18            [discrete value]
                                                12-15                [range value]
                                                /                       [interval/difference]
                                                */5                    [after 5 interval]
N.B
      To getting help about crontab                         #man 5 crontab
      Cron Access Control
        a if allow and deny both are missing                -> only root can add cron job
        b if only deny exist                                -> all user + root except users are mentioned in deny file
        c if only allow exist                               -> root + all user that are mentioned in allow file
        d if both exist                                     -> cron.deny is ignored
           
Example: the following will clear the Apache error log at one minute past midnight ( 00:01 of every day of the
month, of every day of the week ).
1 0 * * *  echo -n "" > /www/apache/logs/error_log

Slash example: the following will run the script /home/user/test.pl every 5 minutes.
*/5 * * * *  /home/user/test.pl

There are several special entries, most of which are just shortcuts, that can be used instead of specifying
the full cron entry:
Entry       Description             Equivalent To
@reboot     Run once, at startup.   None
@yearly     Run once a year         0 0 1 1 *
@annually   (same as @yearly)       0 0 1 1 *
@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)        0 0 * * *
@hourly     Run once an hour        0 * * * *

@reboot can be useful if there is a need to start up a server or daemon under a particular user, or if user
does not have access to the rc.d/init.d files.


insert a username into the format as the sixth field, as whom the specified job will be run (subject to user
existence in /etc/passwd and allowed permissions), but only in the system crontabs
(/etc/crontab and /etc/cron.d/*), not in others which are each assigned to a single user to configure.
The seventh (or sixth if no user field is part of the format) and subsequent fields (i.e., the rest of the line)
specify the command to be run

insert a username into the format as the sixth field, as whom the specified job will be run (subject to user
existence in /etc/passwd and allowed permissions), but only in the system crontabs
(/etc/crontab and /etc/cron.d/*), not in others which are each assigned to a single user to configure.
The seventh (or sixth if no user field is part of the format) and subsequent fields (i.e., the rest of the line)
specify the command to be run

MSC:
#export EDITOR=vi
#s: setenv EDITOR vi

Some Task Example:
backup.sh
cmd | mail -s "Subject of mail" user            [you can just pipe the output to the command mail]
cmd | mail -s "Subject of mail" mail@yahoo.com  [someone not located on the machine, email address]
cmd >> log.file                                 [you can redirect the output to a log file]
cmd >> logfile 2>&1                             [if you want all output stored in the log file]
mail -s "logfile for cmd" <log.file             [mail contents of the file at specified time]

Some Command line History of lecture class
                                                435  vim /etc/crontab
                                                436  ls /etc/cron.hourly/
                                                498  ls /etc/cron.*
                                                499  cd /etc/cron.daily/
                                                500  ls
                                                501  cd
                                                502  ls -l /etc/cron.daily/
                                                503  ls -l /etc/cron.hourly/
                                                504  ls -l /etc/cron.weekly/
                                                505  ls -l /etc/cron.monthly/
                                                506  yum search vnc
                                                507  exit

No comments:

Post a Comment