Crontab

Cron is named after “chronos” the Greek word for time and it is a daemon that runs scheduled jobs and scripts. Crontab or ‘Cron table’ is a file which contains all the jobs details with their schedule time of run.

Any userid which has an entry in the /usr/lib/cron/cron.allow file can run the Crontab. The same user id can run the Crontab if this cron.allow file does not exit and also if this userid does not exist in /usr/lib/cron/cron.deny file. If the cron.deny file has no entry in it, all userid can use the Crontab.

The cron daemon continuously checks every minute for /etc/crontab file and /var/spool/cron/ directory and /etc/cron.* directories. Updates to these files are loaded to the memory by the cron daemon. In case cron daemon is not running for a newly build OS, root user can start the cron service using /sbin/service/crond start. To stop the service, use the command /sbin/service/crond stop

As per their schedules, the scripts will get executed in /etc/cron.hourly/, /etc/cron.daily/, /etc/cron.weekly/, and /etc/cron.monthly/ directories on an hourly, daily, weekly or monthly basis respectively. Except these schedule any other scripts can be run on /etc/cron.d/ directory.

/var/spool/cron/ directory contains user defined crontab files and these files are executed with same userid which are created it.

 

Crontab Commands

  • Crontab -r – deletes the crontab file
  • Crontab -v – shows the time when you last edited the file
  • Crontab -l – view the content of the crontab file
  • Crontab -e – opens the crontab file in edit mode
  • Crontab -u username -l – to view crontab entries of other users.
  • Crontab -u username -e – to view crontab entries of other users.
  • Crontab -l > crontab.current – to copy all the content of latest cron entries to a text file. This crontab.current file can be edited in vi editor mode
  • Crontab crontab.current – To load the updated content to cron memory.

 

 

To specify day, date and time each crontab file follows this standard format.

*     *     *   *    *        /path/script_name to run

|     |     |   |    |

|     |     |   |    L—– day of week [0 – 6] [Sunday=0]

|     |     |   L——- month [1 – 12]

|     |     L——— day of month [1 – 31]

|     L———– hour [0 – 23]

L————- min [0 – 59]

Different ways of scheduling the cron job

*     *     *   *    * /path/Script_name        — To run every minute

0     *     *   *    * /path/Script_name        — To run every hour

15     2     *   *    * /path/Script_name      — To run at 2:15 AM

5     20     *   *    * /path/Script_name      — To run at 8:05 PM

0     11   2,6   *    * /path/Script_name        — To run at 11 am of 2nd and 6th of every month

0     0     *   *    * /path/Script_name        — To run daily at mid night

*     *  5,6,7   *    * /path/Script_name        — To run every minute on May, June and July

30     4     3   2    * /path/Script_name        — To run every year 3rd Feb at 4:30 AM

*     *     *   *    * /path/Script_name; /path/2nd_Script_name      — To run 2 tasks on a single cron [use semicolon]

@yearly               /path/Script_name        — To run on first minute of every year

@monthly          /path/Script_name        — To run on first minute of every month

@weekly            /path/Script_name        — To run on first minute of every week

@daily        /path/Script_name                — To run on first minute of every day

@hourly              /path/Script_name        — To run on first minute of every hour

@reboot             /path/Script_name        — To run after reboot on system start up

46    0-23/2     *   *    *    /path/Script_name        — To run at 46 minutes starting at 2 am, 4am, 6am etc

Also read about “at” Scheduler here