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


Read More Add your Comment 0 comments

What is Linux cron job ?



Cron is the time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration.

Linux Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis.


crontab

The command to create/edit, list, and remove cron jobs is crontab. If you call it with the -u option, it specifies the name of the user whose crontab is to be tweaked. If this option is not given, crontab examines crontab

Examples:
crontab -l
lists the cron jobs of the user as that you are currently logged in:
lists all cron jobs of exampleuser.
crontab -e
let's you create/modify the cron jobs of the user as that you are currently logged in (I'll come to the syntax in the next chapter).
crontab -u OSuser -e
let's you create/modify the cron jobs of OSuser
crontab -r
deletes all cron jobs of the user as that you're currently logged in.
crontab -u OSuser -r
deletes all cron jobs of OSuser


A cron job consists out of six fields:
<minute> <hour> <day of month> <month> <day of week> <command>

Linux Crontab Format
MIN HOUR DOM MON DOW CMD

*    *    *    *    *  command to be executed
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 7) (0 or 7 are Sunday, or use names)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)


Read More Add your Comment 0 comments
 

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