Wednesday, March 31, 2010

What is crontab?



   UNIX operating systems contain a daemon called 'cron'. The job of the cron daemon is to wake up every minute and execute all the tasks scheduled for the particular minute.  The tasks to be executed by the cron are present in a file called 'crontab' file.

   The crontab file is present for every user in the UNIX box.  Generally, shell scripts to be executed on a timely basis is scheduled as part of the crontab. However, even standalone UNIX commands can also be scheduled as part of the crontab.

  UNIX has a command called 'crontab'. This crontab command can be used to create new crontab tasks and also to check the tasks scheduled in cron.

crontab usage:

  1. To check the crontab activities scheduled,

    #crontab -l
    4 5 * * sat     echo "Execute at 4 after 5 every saturday"
    #

    The above displayed task will execute at 4hours 5 mins on every saturday.

  2. To create a new task in crontab;

   #crontab -e
    20 00 * * *  tcsh ~gpr/bin/create_ctags v94_0
   #

    On typing the command 'crontab -e', a vi like editor opens up. We need to edit the file and put the above shown contents as editing any file in vi. And finally, save and quit the file. The above command creates a cron job to execute at 20hours everyday.

  Syntax of the crontab file:

   hh mm dom mon dow  

    hh   - hour   (0-24)
    mm - minute (0-59)
    dom - day of the month   (1-31)
    mon - month  (0-12)
    dow  - day of the week (0-6   0-Sunday  6-Saturday. Also, we can use the names, like sun, mon)

  The following are the important things to keep in mind:

  1. While testing cron activities, dont schedule any activity in the immediate next minute. Schedule atleast 2mins later. At times, cron does not refresh properly.

  2. When cron executes any crontab activity, it spawns a new shell and executes. This new shell does not inherit all the environment variables of the user. Instead, very few environment variables are set. The following are few of them: SHELL(/bin/sh), USER, LOGNAME and HOME

3. Because of the above reason, a shell script running successfully at the command prompt may not run successfully when scheduled in crontab. In order to overcome this, the user has to include the sourcing of his .login or .profile file inside the shell script.

No comments:

Post a Comment