Thursday, October 23, 2014

Schedule Tasks In Linux With CRON

Schedule Tasks In Linux With CRON
                                      There is an utility in Linux known as CRON with which you can start your jobs automatically at a desired time and schedule them to get executed periodically.
                                     Cron utility consists of two parts: The cron daemon and the cron configuration files. Cron daemon is just like any service that is started automatically whenever your system boots. Cron configuration files hold the information of what to do and when to do
  1. Edit a Crontab Entry :- crontab -e
  2. Select Favorite editor.If You select any other option, use the following command to open in VI editor
            export VISUAL=vi
     3. Create a new crontab file from another user
            crontab -l > name  (Created in Present Working Directory)
     4. Listing the Cron Jobs :- crontab –l
     5. To enlist the cron jobs for a particular user :- crontab –u [USER] –l
Example :- crontab –u root –l
     6. For deleting Crontab entry : - crontab -r
     7. For activating crontab entry : - crontab crontab name
Crontab syntax :-
                                           A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval. See below:
  • 1: Minute (0-59)
  • 2: Hours (0-23)
  • 3: Day (0-31)
  • 4: Month (0-12 [12 == December])
  • 5: Day of the week(0-7 [7 or 0 == sunday])
  • /path/to/command - Script or command name to schedule
Easy to remember format:
*     *     *     *     *  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)

Sample :-
  1. Created a bash script for making directory
root@IPAM:/home/ictops#vi job.sh
mkdir -p /home/ictops/test1
mkdir -p /home/ictops/test1
      2. Provide Executable permission :- chmod +x /home/ictops/job.sh
      3. Add this batch file “job.sh” into crontab entry for sheduling task
        root@IPAM:/home/ictops#crontab -l > mycron   
            no crontab for root
        root@IPAM:/home/ictops#crontab -e
no crontab for root - using an empty one
root@IPAM:/home/ictops#crontab -l > mycron   
root@IPAM:/home/ictops#crontab  mycron

No comments:

Post a Comment

bloggerwidgets