whenever gem and scheduling a job every n min starting at an offset

HeretoLearn picture HeretoLearn · Jun 10, 2011 · Viewed 21.2k times · Source

For staggering purposes I am trying to schedule jobs an a 2 minute offset that run every 5 mins. That is I want 1 job to run 1,6,11,16.. and the other one to run at 2,7,12,17...

I couldn't find an example to do this. So I tried:

every 5.minutes, :at=> 1 do
 command "echo 'you can use raw cron sytax too'"
end 

This seems to work but all the ':at' examples look to be expecting a time in a string format. Is the above valid to do or is just happens to work and the every option doesn't really support a starting time.

Answer

Pan Thomakos picture Pan Thomakos · Jun 10, 2011

It sounds like you have a dependency between the two jobs, so there are two ways I think you can handle this. If you want to run at 1,6,11,16 and so on, as your question suggests, then simply use raw cron syntax:

every '0,5,10,15,20,25,30,35,40,45,50,55 * * * *' do
  command "echo 'you can use raw cron syntax one'"
end

every '1,6,11,16,21,26,31,36,41,46,51,56 * * * *' do
  command "echo 'you can use raw cron syntax two'"
end

But it's probably better to execute the second job once the first one is done. This should ensure that the jobs don't overlap and that the second only runs when after the first completes.

every 5.minutes do
  command "echo 'one' && echo 'two'"
end