Exploring cron

Exploring Cron – II

In our previous blog we have overview of how to create a cron expression.

This blog will walk you through some solid example, which will help you to get detailed undertstanding of crontab (Chronos tab or chornological table)

1. How do I see the current cron tab entry?

A) From command prompt we need to fire the command

sudo crontab -l

2. How do I edit the entry of cron tab?

A) -e switch of the cronttab will help you to edit the entries (default vi editor)

sudo crontab -e

3. How do I execute every minute?

A) * * * * * /path/to/your/script.sh

4. How do I execute every saturday 2 AM?

A) 0 2 * * 6 /path/to/your/script.sh (Here 6 represent Sarturday and 2 represent the 2nd Hour i.e 2 AM

5. How do I execute a script every 20 minutes?

A) 0,20,40 * * * * /path/to/your/script.sh 

Keywords in Crontab entries.

@reboot     this will invoke to execute your script when the system startup
@yearly     this will run once in a year
@annually   this will invoke the script once in a year
@monthly    this will invoke the script once in a month
@weekly     this will invoke the script once in a week
@daily      this will invoke the script once in a day
@midnight   this will invoke the script daily
@hourly     this will invoke the script every hour

Exploring cron Expression

cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of cron.

CronTrigger uses “cron expressions” for scheduling .

* * * * * command to execute
– – – – –
¦ ¦ ¦ ¦ ¦
¦ ¦ ¦ ¦ ¦
¦ ¦ ¦ ¦ +—– day of week (0 – 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
¦ ¦ ¦ +———- month (1 – 12)
¦ ¦ +————— day of month (1 – 31)
¦ +——————– hour (0 – 23)
+————————- min (0 – 59)

Some Example according to cron Expresssions :-

Some cron examples ,

Expression Execution Detail Comment
  5  2  *  *  6  This CRON job starting at Current-DateTime (now), it would run:

  1. 2014-11-29 02:05:00
  2. 2014-12-06 02:05:00
  3. 2014-12-13 02:05:00
  4. 2014-12-20 02:05:00
  5. 2014-12-27 02:05:00
 

 

Once in a week

  5  2  *  *  *  This CRON job starting at Current-DateTime (now), it would run:

  1. 2014-11-26 02:05:00
  2. 2014-11-27 02:05:00
  3. 2014-11-28 02:05:00
  4. 2014-11-29 02:05:00
  5. 2014-11-30 02:05:00

 

 

 

Once in a Day

5  */12  *  *  *  This CRON job starting at Current-DateTime (now), it would run:

  1. 2014-11-25 12:05:00
  2. 2014-11-26 00:05:00
  3. 2014-11-26 12:05:00
  4. 2014-11-27 00:05:00
  5. 2014-11-27 12:05:00

 

 

 

Twice in a Day

5  */6  *  *  * This CRON job starting at Current-DateTime (now), it would run:

  1. 2014-11-25 12:05:00
  2. 2014-11-25 18:05:00
  3. 2014-11-26 00:05:00
  4. 2014-11-26 06:05:00
  5. 2014-11-26 12:05:00
  6. 2014-11-26 18:05:00
  7. 2014-11-27 00:05:00

 

 

 

4-times a Day