whenever icon indicating copy to clipboard operation
whenever copied to clipboard

Specify day on monthly jobs

Open tsommer opened this issue 17 years ago • 13 comments

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...

tsommer avatar Jun 18 '09 00:06 tsommer

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

javan avatar Jun 18 '09 00:06 javan

no, the complete output was:

0 9 18 * * command

tsommer avatar Jun 18 '09 00:06 tsommer

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

javan avatar Jun 18 '09 00:06 javan

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

tsommer avatar Jun 18 '09 00:06 tsommer

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

javan avatar Jun 18 '09 00:06 javan

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

aparmar avatar Jun 18 '09 01:06 aparmar

Thanks Javan.

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

tsommer avatar Jun 18 '09 01:06 tsommer

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

aparmar avatar Jun 18 '09 01:06 aparmar

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

javan avatar Jun 18 '09 02:06 javan

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

eeng avatar Feb 01 '11 14:02 eeng

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 avatar Feb 01 '11 14:02 javan

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

eeng avatar Feb 01 '11 15:02 eeng

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

jinyi0313 avatar May 06 '18 02:05 jinyi0313