≡ Menu

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

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.

Add your comment

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

Comments on this entry are closed.

  • Gary K July 8, 2011, 4:58 am

    Interesting Article… Thanks

  • haotian July 8, 2011, 8:25 am

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

  • Ashok July 8, 2011, 8:35 am

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

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

    Hi Ramesh,

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

    Regards,
    Júlio.

  • yara August 26, 2011, 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

  • abcdef February 8, 2012, 3:33 am

    Very useful thanks a lot

  • Narayan June 12, 2012, 4:40 pm

    very helpful.

    Regards,
    Narayan

  • Stephane July 11, 2012, 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

  • George July 14, 2012, 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.

  • George July 14, 2012, 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.

  • Eric Nguyen October 22, 2012, 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

  • Muthu October 29, 2012, 1:29 pm

    Very very Help ful

  • Eric December 9, 2012, 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.

  • Ashwath Nadahalli January 28, 2013, 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.

  • Anonymous March 3, 2013, 10:42 pm

    hi very useful

  • SSY March 7, 2013, 2:15 pm

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

  • Ricardo May 9, 2013, 10:56 am

    Thaks Ramesh ! It helped me a lot !

  • ANJANI KUMAR May 24, 2013, 4:12 am

    Thanks Ramesh,
    It was helpful and easy to understand.

  • Ananda May 28, 2013, 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

  • yasaswi October 4, 2013, 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

  • Anonymous October 15, 2013, 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

  • sathish July 1, 2014, 4:39 am

    Very useful, neat and simple explaination

  • Bill September 12, 2014, 11:35 am

    doesn’t seem to be running for some reason, i put it to write an to an error file and its not even creating the file.

    any pointers?

  • TDMike November 7, 2014, 4:32 pm

    Thanks, this was exactly a solution I was looking for.

  • shufil December 3, 2014, 5:07 am

    HI Ramesh ,

    Am looking a crone job , its need to run every 2 minite , i tested following two bash scripts but not working, script is running manually as root and user , i added user cron and root cron. and file permission added correctly with executive bit .
    02 * * * * /bin/sh /path/test.sh
    #!/bin/bash
    #service=node

    if (( $(lsof -i:3007 | wc -l) > 0 ))
    then
    nohup node test_app.js &
    else
    echo “$service is running!!!”
    fi
    ____________________
    #!/bin/bash
    #service monitoring
    /bin/netstat -tulpn | awk ‘{print $4}’ | awk -F: ‘{print $4}’ | grep ^3007$ > /dev/null 2>/dev/null
    a=$(echo $?)
    if test $a -ne 0
    then
    echo “node.js service down”
    nohup node test_app.js & > /dev/null 2>/dev/null
    else
    sleep 0
    fi
    __________________

    both are not working in crone ,crone log is not given related this, service port is 3007 and restart command node.js service down .
    so please advice me and help me about this problem

    Shufil

  • Harish December 17, 2014, 4:20 pm

    How can i verify that my cron job is running in every 6 minutes.
    please do advise

  • Jack Wade February 21, 2015, 5:58 am

    You forgot to mention the handy dandy shortcuts:

    @reboot
    @hourly
    @daily
    and some others

  • Peeyush Kumar May 25, 2017, 7:22 am

    I want to know that if we want to run cron job on 9AM and 9PM both time then what pattern will be used by me. Please help as soon as possible. Thanks in advance.