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

by Ramesh Natarajan on July 8, 2011

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.

Create a shell script every-5-seconds.sh using bash while loop as shown below.

$ cat every-5-seconds.sh
#!/bin/bash
while true
do
 /home/ramesh/backup.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 5th weekday

This example is not about scheduling “every 5 days”. But this is for scheduling “every 5th weekday”.

The 5th field is DOW (day of the week). If you specify * in this field, it runs every day. To run every Friday, specify either 5 of Fri in this field.

The following example runs the backup.sh every Friday at midnight.

0 0 * * 5 /home/ramesh/backup.sh
(or)
0 0 * * Fri /home/ramesh/backup.sh

You can either user number or the corresponding three letter acronym for the weekday as shown below.

  • 0=Sun
  • 1=Mon
  • 2=Tue
  • 3=Wed
  • 4=Thu
  • 5=Fri
  • 6=Sat

Note: Get into the habit of using Fri instead of 5. Please note that the number starts with 0 (not with 1), and 0 is for Sun (not Mon).

5. 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. Probably you may want to run the job on 5th month (May), and 10th month (Oct).

The fourth field is for Months. If you specify * in this field, it runs every month. To run for the specific month, you have to specify the number that corresponds to the month. For example, to run the job on May and Oct, you should specify 5,10 (or) you can simply use the 3 letter acronym of the month and specify May,Oct.

The third field is for DOM (Day of the Month). If you specify * in this field, it runs every day of the month. If you specify 1 in this month, it runs 1st of the month.

The following example runs the backup.sh twice a year. i.e 1st May at midnight, and 1st Oct at midnight.

0 0 1 5,10 * /home/ramesh/backup.sh
(or)
0 0 1 May,Oct * /home/ramesh/backup.sh

Note: Don’t make the mistake of specifying 5-10 in the 4th field, which means from 5th month until 10th month. If you want only 5th and 10th month, you should use comma.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 21 comments… read them below or add one }

1 Gary K July 8, 2011 at 4:58 am

Interesting Article… Thanks

2 haotian July 8, 2011 at 8:25 am

* */5 * * * /home/ramesh/backup.sh
not
01 */5 * * * /home/ramesh/backup.sh ?

3 Ashok July 8, 2011 at 8:35 am

Hi Ramesh,
We had an interesting scenario for executing the cronjob on specific days in a month (here)

4 Júlio Hoffimann Mendes July 8, 2011 at 6:34 pm

Hi Ramesh,

Thank you for your objective articles. They’re very helpful.

Regards,
Júlio.

5 yara August 26, 2011 at 1:48 am

If i want run task into period 8:00 am to 10:00 pm juste evry 2 hours its correct to make it :
0 8-22/2 * * * command
or
0 8-22,2 * * * command

Thnaks

6 abcdef February 8, 2012 at 3:33 am

Very useful thanks a lot

7 Narayan June 12, 2012 at 4:40 pm

very helpful.

Regards,
Narayan

8 Stephane July 11, 2012 at 10:04 pm

Hi, Thank you for this useful information! Would you know how I could possibly schedule a command to run every 4th WORKING DAY of every month though? Cheers Stephane

9 George July 14, 2012 at 4:03 am

Stephane,

My internet connection went, so here is a short version of what I tried to send:

0 0 4 * 4-5 command_here
0 0 5 * 4 command_here
0 0 6 * 1-4 command_here

If the month starts on mon or tue, the first one will trigger for that month on the 4th (it checks for the 4th being a thur or fri).

If the month starts with a sun, the second line will trigger on the 5th which will be a thur.

If the month starts with a wed-sat, then the 3rd line will trigger, running on the 6th which will be the 4th weekday (there will be a sat/sun prior to the 6th)

This will work if you want the 4th non-weekend day of the week. If you want WORKING DAY to account for holidays, then you need to run a wrapper script that check for such things and then runs the desired script as appropriate. cron doesn’t check for holidays. ;)

I’m not a cron guru, so if someone can follow up with confirmation. I think I have it right.

10 George July 14, 2012 at 4:15 am

On note regarding my previous comment and in general cron jobs starting with 0 0 ….

The actual “minute hour” (0 0 ….) is arbitrarly set to midnight. Unless your script needs to be run at midnight, you might want to tweek this in the event that you have multiple cron tasks that have leading zeros. You might want to even the load rather than have a bunch of script all starting at the same time.

I usually try to space them through the early am. You could replace the leading 0 0 with 20 1 and your job would run at 1:20 am on each day that it is triggered.

Again, I’m not a cron guru, but I don’t think I would want half a dozen scripts each running at the same time, potentially trying to download information. Personally, my internet connection might not take it.

11 Eric Nguyen October 22, 2012 at 8:37 pm

Hi Ramesh,

Thanks for sharing this tip.
A rather small and off-topic question: how do you run shell script without bash?

I have ec2-automate-backup.sh from colinbjohnson on Github but I can only it as
bash ec2-automate-backup.sh …

running either
ec2-automate-backup.sh
or
ec2-automate-backup
will give command not found error

Thanks,
Eric

12 Muthu October 29, 2012 at 1:29 pm

Very very Help ful

13 Eric December 9, 2012 at 6:22 am

For the case when you want to run the script /home/ramesh/backup.sh every 5 seconds, if it takes say 2 sec to execute, you would actually end-up running the script every 5+2=7 seconds, not every 5 seconds.

So if you’re certain that the script won’t last more than 5 seconds, it’s probably better to spawn it in the background.

14 Ashwath Nadahalli January 28, 2013 at 4:07 am

how do I set up a cron job which starts at 9.15am and ends at 3.45pm on everyday?
I went through a lot of websites, but no solution. I would appreciate If I can get it here.

Regards.

15 Anonymous March 3, 2013 at 10:42 pm

hi very useful

16 SSY March 7, 2013 at 2:15 pm

This is very helpful. How to setup a cron job to run every other Saturday?

17 Ricardo May 9, 2013 at 10:56 am

Thaks Ramesh ! It helped me a lot !

18 ANJANI KUMAR May 24, 2013 at 4:12 am

Thanks Ramesh,
It was helpful and easy to understand.

19 Ananda May 28, 2013 at 5:12 am

Can somebody please tell me how to write cron expression to run a job every 10 minutes from 8.45 pm to 11.45 pm

20 yasaswi October 4, 2013 at 4:03 am

Hi

This document is very use full to create a cronjob. i created a cronjob
45 12 * * * /usr/local/nagios/libexec/check_tripwire.sh

the script file consists of (Check_tripwire.sh)

tripwire –check
twprint -m r -t 0 -L /etc/tripwire/local.key -s -c /etc/tripwire/tw.cfg -r var/lib/tripwire/report/demo-*.twr > /tmp/report.txt
twprint –print-report –twrfile /var/lib/tripwire/report/demo-*.twr > /tmp/report1.txt
cat /tmp/report.txt /tmp/report1.txt > /tmp/test.txt

the script file is working properly while it is exicuting (./check_tripwire.sh) seperately.

but the same job is assigned to crontab i am getting the following errors

/usr/local/nagios/libexec/check_tripwire.sh: line 7: tripwire: command not found
/usr/local/nagios/libexec/check_tripwire.sh: line 8: twprint: command not found
/usr/local/nagios/libexec/check_tripwire.sh: line 13: twprint: command not found

can u please suggest me how to solve those errors…

thanks in advance

21 Anonymous October 15, 2013 at 5:24 am

Hello yasaswi,

put your whole path to the executed program in all commands, e.g. :

/path/of/program/to/execute/tripwire –check

Regards

Leave a Comment

Previous post:

Next post: