How to Run Cron Every 5 Minutes, Seconds, Hours, Days, Months
Source: thegeekstuff.com
Question: How do I execute certain shell script at a specific intervals in Linux using cron job? Provide examples using different time periods.
Answer: Crontab can be used to schedule a job that runs on certain internal. The example here show how to execute a backup.sh shell script using different intervals.
Also, don’t forget to read our previous crontab article that contains 15 practical examples, and also explains about @monthly, @daily, .. tags that you can use in your crontab.
1. Execute a cron job every 5 Minutes
The first field is for Minutes. If you specify * in this field, it runs every minutes. If you specify */5 in the 1st field, it runs every 5 minutes as shown below.
*/5 * * * * /home/ramesh/backup.sh
Note: In the same way, use */10 for every 10 minutes, */15 for every 15 minutes, */30 for every 30 minutes, etc.
2. Execute a cron job every 5 Hours
The second field is for hours. If you specify * in this field, it runs every hour. If you specify */5 in the 2nd field, it runs every 5 hours as shown below.
0 */5 * * * /home/ramesh/backup.sh
Note: In the same way, use */2 for every 2 hours, */3 for every 3 hours, */4 for every 4 hours, etc.
3. Execute a job every 5 Seconds
Cron job cannot be used to schedule a job in seconds interval. i.e You cannot schedule a cron job to run every 5 seconds. The alternative is to write a shell script that uses ‘sleep 5′ command in it.