Cron resources

Cron is the mechanism on UNIX-like systems which runs scheduled jobs, sometimes known as batch jobs, at specified times or intervals. For example, system housekeeping tasks such as log rotation, or checking for security updates, are run from cron. The details of what to run and when to run it are kept in a specially-formatted file called crontab (short for cron table).

Puppet provides the cron resource for managing scheduled jobs, and we saw an example of this in the run-puppet manifest we developed in Chapter 3, Managing your Puppet code with Git (run-puppet.pp):

cron { 'run-puppet':
  command => '/usr/local/bin/run-puppet',
  hour    => '*',
  minute  => '*/15',
}

The title run-puppet identifies the cron job (Puppet writes a comment to the crontab file containing this name, to distinguish it from other, manually-configured cron jobs). The command attribute is the command for the cron resource to run, and the hour and minute attributes specify the time (*/15 is cron syntax meaning every 15 minutes).

Tip

For more information about cron and the possible ways to specify the times of scheduled jobs, run the man 5 crontab command.

Attributes of the cron resource

The cron resource has a few other useful attributes which are shown in the following example (cron.pp):

cron { 'cron example':
  command     => '/bin/date +%F',
  user        => 'vagrant',
  environment => ['[email protected]', 'PATH=/bin'],
  hour        => '0',
  minute      => '0',
  weekday     => ['Saturday', 'Sunday'],
}

The user attribute specifies who should run the cron job (if none is specified, the job runs as root). If the environment attribute is given, it sets any environment variables the cron job might need. A common use for this is to e-mail any output from the cron job to a specified e-mail address, using the MAILTO variable.

As before, the hour and minute attributes set the time for the job to run, while you can use the weekday attribute to specify a particular day, or days, of the week. (The monthday attribute works the same way, and can take any range or array of values between 1-31 to specify the day of the month.)

Tip

One important point about cron scheduling is that the default value for any schedule attribute is *, which means all allowed values. For example, if you do not specify an hour attribute, the cron job will be scheduled with an hour of *, meaning that it will run every hour. This is generally not what you want. If you do want it to run every hour, specify hour => '*' in your manifest, but otherwise, specify the particular hour it should run at. The same goes for minute. Accidentally leaving out the minute attribute and having a job run 60 times an hour can have amusing consequences, to say the least.

Randomizing cron jobs

If you run a cron job on many nodes, it's a good idea to make sure that the job doesn't run everywhere at the same time. Puppet provides a built-in function, fqdn_rand(), to help with this; it provides a random number up to a specified maximum value, which will be different on each node, because the random number generator is seeded with the node's hostname.

If you have several such jobs to run, you can also supply a further seed value to the fqdn_rand() function, which can be any string, and which will ensure that the value is different for each job (fqdn_rand.pp):

cron { 'run daily backup':
  command => '/usr/local/bin/backup',
  minute  => '0',
  hour    => fqdn_rand(24, 'run daily backup'),
}

cron { 'run daily backup sync':
  command => '/usr/local/bin/backup_sync',
  minute  => '0',
  hour    => fqdn_rand(24, 'run daily backup sync'),
}

Because we gave a different string as the second argument to fqdn_rand for each cron job, it will return a different random value for each hour attribute.

The range of values returned by fqdn_rand() includes 0, but does not include the maximum value you specify. So in the previous example, the values for hour will be between 0 and 23, inclusive.

Removing cron jobs

Just as with user resources, or any type of resource, removing the resource declaration from your Puppet manifest does not remove the corresponding configuration from the node. In order to do that, you need to specify ensure => absent on the resource.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.98.249