How to Run CRON JOB every 5 Minutes, Hours, Seconds, Months ?




How to Run Cron Every 5 Minutes, Hours, Seconds, Months


A cron job consists out of six fields:
<minute> <hour> <day of month> <month> <day of week> <command>
              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sun, or use names)

1. Execute a cron job every 5 Minutes

The first field is for Minutes. If you specify * in this field, it runs every minutes. 
*/5 * * * * /home/user/test.sh

2. Execute a cron job every 5 Hours

The second field is for hours. If you specify * in this field, it runs every hour. 
0 */5 * * *  /home/user/test.sh

3. Execute a job every 5 Seconds

There is not direct method to write cron job to be scheduled every 5 seconds. The alternate way is to write a shell script that uses ‘sleep 5′ command in it. Example is below:
$ cat every-5-seconds.sh
#!/bin/bash
while true
do
 /home/user/test.sh
 sleep 5
done
Now, execute this shell script in the background using nohup as shown below. This will keep executing the script even after you logout from your session. This will execute your backup.sh shell script every 5 seconds.
$ nohup ./every-5-seconds.sh &

4. Execute a job every 5 months

There is no direct way of saying ‘every 5 months’, instead you have to specify what specific months you want to run the job. The following example runs the test.sh twice a year. i.e 1st May at midnight, and 1st Oct at midnight.
0 0 1 5,10 * /home/user/test.sh


Share your views...

0 Respones to "How to Run CRON JOB every 5 Minutes, Hours, Seconds, Months ?"

Post a Comment

 

© 2010 Codes & Concepts All Rights Reserved Thesis WordPress Theme Converted into Blogger Template by Hack Tutors.info