More dynamic cronjobs

(george.mand.is)

53 points | by 0928374082 5 hours ago

6 comments

  • AndrewDavis 3 hours ago
    Great post. And if you want some control support for your cronjobs perl App::Cronjob[1] can provide features such has exclusive locking, so a job won't run if the previous run is still going, or provide a timeout, and some options for sending mail on success or failure

    [1]https://metacpan.org/pod/App::Cronjob https://metacpan.org/dist/App-Cronjob/view/bin/cronjob

  • bblb 1 hour ago
    Also check out the 'chronic' command from moreutils. No more dev nulls.
  • hermannj314 29 minutes ago
    I learned something cool about cron filtering and a nice api I didn't know existed - date.nager.at
  • WolfCop 3 hours ago
    Does anyone maintain a programmatically accessible list of holidays for their company? Similar to the HOLIDAYS.txt in the article, but it would allow for things like “don’t run this the day before or during a company holiday.”

    I work at a company with different holidays in certain countries, which would complicate things, and require something more structured than a list of dates. But having that accessible could be useful.

    Has anyone tackled that, or come across a solution?

    • glawre 45 minutes ago
      At our company we have enough systems reliant on holiday dates that we have a Holiday system that emits events when there are changes.

      This happens surprisingly often, given that religious dates change and there are holidays/closures for storms in some regions.

    • sublinear 17 minutes ago
      I had something come up recently that I think sounds similar. That project needs several time-sensitive jobs. When any one of them runs, the first thing it does is check a holidays.json file.

      It parses the file using jq and compares its entries with the current time according to GNU date. At the root is the names of the jobs. Each job has its own list of holidays. Each of these holiday items in the job's respective list has keys for the display name of the holiday, the formatted date to compare to, and in a few cases the ISO day-of-week and a string containing a modulo arithmetic function (e.g. don't run the friday before Christmas, etc.).

      Sorry, yes that means I call eval on that string and yes that means some of these are repeated in the same file under the arrays for the other jobs. Also, such lists will have to be maintained and the exact dates cannot be known ahead of time beyond about a year since people can change their minds for various reasons (think bank holidays). Depending on your use case you may also want to define a start time and end time for a window of when this should or shouldn't run (i.e. business hours).

      I don't know if that helps. I know it's hacky, but I don't think there's a nice way to handle things like "second monday after 4th of july, but if the 4th also happens to be monday then it should instead be the second tuesday". God help you if you also need to handle each holiday being observed in different timezones.

    • jaredsohn 3 hours ago
      Ruby has https://github.com/bokmann/business_time but when I looked at it, custom code was needed to calculate holidays that were offset because they are on the weekend.
  • stevenjgarner 3 hours ago
    This is great! I'm sure like a lot of programmers, I had been fulfilling the requirement for similar conditional logic by having a simple recurring cron job run other code or database queries with the conditional logic that this post demonstrates can be done directly in cron.
  • victorbjorklund 4 hours ago
    Cool. Had no idea you could run commands inside a CRON expression.
    • garganzol 4 hours ago
      Running a command is the main idea of cron. In this case, the author runs composite commands like:

          test && action
      
      Where 'test' is another shell command that returns 0 or 1. This is not a special cron syntax, it's just the inherent capability of the Unix shell.

      In any case, this whole approach is very clever and shows the beauty of The Unix Way.