Whenever: Specify day on monthly jobs

Created on 18 Jun 2009  ·  13Comments  ·  Source: javan/whenever

When i tried to set a time with a monthly cronjob i had to use '1.month'.
But this created a cronjob for every month on todays day, eg.

0 9 18 * * ..... instead of
0 9 1 * * ..... which would fire on the first day of the month...

Most helpful comment

If you dont want to give the full date, you could try something like this:

every 1.month, :at => "start of the month at 9am"
This will produce the intended "0 9 1 * *". It will also keep your code a little cleaner :)

Hope that helps.

-AP

All 13 comments

are you showing all of the cron output there? your first example has a 9 in the hour place and 18 in the day.

# .---------------- minute (0 - 59) 
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ... 
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat 
# |  |  |  |  |
# *  *  *  *  *  command to be executed

no, the complete output was:

0 9 18 * * command

can you paste your schedule.rb file? something seems off.

every 1.month, :at => "9 AM" do
  command "..."
end

Ah, I see the problem (other than me not having good docs in this area). When you use :at with 1.month, the day, hour, and minute get parsed from the time. So when you just use a time like 9am, it assumes the day you mean is today.

One way to correct this is give it a full date. I admit that this looks ugly, but it should work: every 1.month, :at => "January 1st 9am"

It will ignore the "January", but it's necessary for a successful parse (Whenever uses the Chronic gem for parsing dates and times).

Hope that helps.

the

If you dont want to give the full date, you could try something like this:

every 1.month, :at => "start of the month at 9am"
This will produce the intended "0 9 1 * *". It will also keep your code a little cleaner :)

Hope that helps.

-AP

Thanks Javan.

As you said, thats not good looking but it works!

tsommer...the above solution I posted should work. I'm not sure if you looked at it.

"start of the month at 9am" -- that rules! thanks.

I need to express this cron job "0 0 27-31 * *". Is it posible with the current syntax?

Maybe would be better to provide another method in the DSL to pass in cron jobs in the normal cron syntax, something like:

cron "0 0 27-31 * *" do
   # ...
end

I agree that a raw syntax option would be nice (patches welcome!). In the meantime you can do this with a dash of Ruby.

every 1.month, :at => (27..31).to_a.map { |d| "12am January #{d}" } do
  command "..."
end

Javan thanks for the fast response.
Currently I'm using the following patch. Maybe the regexp could need some improvements but it works for me now.

class Whenever::Output::Cron
  CRON_EXP = /[^\s]+\s[^\s+]\s[^\s]+\s[^\s]+\s[^\s]+/

  def time_in_cron_syntax
    case @time
      when CRON_EXP then @time
      when Symbol then parse_symbol
      when String then parse_as_string
      else parse_time
    end
  end
end

Then I can do:

every '0 0 27-31 * *' do
  ...
end

how i create every 2 month, end of day(31th), 11 59pm do cron?
every 2.month, at 59 11 L * 0

Was this page helpful?
0 / 5 - 0 ratings