Efficiently distributing cron jobs

When you have many servers executing the same cron job, it's usually a good idea not to run them all at the same time. If all the jobs access a common server, it may put too much load on that server, and even if they don't, all the servers will be busy at the same time, which may affect their capacity to provide other services.

Puppet's inline_template function allows us to use some Ruby logic to set different runtimes for the job, depending on the hostname.

How to do it…

  1. Add the following to a node:
    define cron_random( $command, $hour ) { cron { $name: command => $command, minute => inline_template("<%= (hostname+name).hash.abs % 60 %>"), hour => $hour, ensure => "present", } } cron_random { "hello-world": command ...

Get Puppet 2.7 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.