6

I have a cron job on CentOS that I want to execute every 3 minutes but I have many other cronjobs that run at every 3 minutes starting from 0, 3, 6, 9...

So, to avoid my server getting too overloaded, I wanted some of my crons to run at every 3 minutes but starting at 1 minute and so on: 1, 4, 7...

My crons are usually like this:

*/3 * * * * 

How can I do this?

17

1-59/3 is the more typical and concise way to specify it, meaning "every 3 minutes starting from 1".

7

In addition to heemayl's answer, you could list the minutes you want, though it's less compact and probably more error prone than the existing way:

1,4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58 * * * * some_command
  • nice, I didnt know that! :) And what would this do 1,4,7 2,5 * * * ? Would it work as expected, I mean, would this cron run at 2 hour 1 minute, 4 minute, 7 minute and at 5 hour 1 minute, 4 minute and 7 minutes? – Samul Oct 31 '15 at 15:22
  • @Samul yup, that's just what it would do – Eric Renouf Oct 31 '15 at 16:45
3

You can use sleep 60 as the first command so that the command you want to run, actually runs after 1 minute while keeping the cron time as */3:

*/3 * * * * sleep 60 && some_command
  • nice alternative but does it add any overhead to cron? I already have about 140 crons running on this server and they are all very optimized. Will using this sleep 60 create any overhead? I mean, if I use it on 70 of my crons, and keep the other 70 without sleep, will it be "easy" to my server run? – Samul Oct 31 '15 at 15:21
  • @Samul Each sleep 60 means an extra process that will be finished after 60 seconds automatically (given everything goes well)..depending on your system's load this could or couldn't be a problem..in normal cases and stable systems this shouldn't be a problem even with 140 jobs.. – heemayl Oct 31 '15 at 16:01
  • Also, sleep literally just sleeps. All it does is (basically) ask the OS to wake it up after the specified amount of time. Don't believe me? Try strace sleep 60 to see what it really does. – a CVn Oct 31 '15 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.