A collection of task oriented solutions in Puppet

 

Human Friendly cron times

Challenge

You want to schedule a cronjob with a human friendly time specification.

Solution

class special_cron_time {

  cron { 'freshen_repos':
    ensure  => 'present',
    command => '/usr/local/sbin/freshen_repos',
    user    => 'root',
    special => 'daily', # translates to "0 0 * * *"
  }

}

Explanation

A number of cron implementations allow you to specify the execution time as a human friendly 'nickname' rather than a set of numbers. These nicknames include daily, weekly and on system reboot. Using one of these special specifications in puppet is surprisingly simple:

class special_cron_time {

  cron { 'freshen_repos':
    ensure  => 'present',
    command => '/usr/local/sbin/freshen_repos',
    user    => 'root',
    special => 'daily', # translates to "0 0 * * *"
  }

}
Notice: /Stage[main]/Special_cron_time/Cron[freshen_repos]/ensure: created

# we can then check the cronjobs contents:

> sudo cat /var/spool/cron/root
# HEADER: This file was autogenerated at 2018-03-04 11:43:50 +0000 by puppet.
# Puppet Name: freshen_repos
@daily /usr/local/sbin/freshen_repos

The only difference when using the cronjob type in this way is the replacement of the usual time specifying attributes (minute, hour, etc.) with special. All of the other values, such as command and user are provided in the usual way. To help avoid invalid nicknames puppet performs validation on the value provided:

# The '@' is not required
Error: Parameter special failed on Cron[freshen_repos]:
  Invalid special schedule "@daily" at /snip/crons.pp:3

# unknown nicknames also fail validation
Error: Parameter special failed on Cron[freshen_repos]:
  Invalid special schedule "whenever" at /snip/crons.pp:3

The most common validation failure is specifying the unneeded '@' in the value. Puppet will add one for you.

While most cron daemon implementations support these nicknames if you use a less common one the easiest way to check if your cron daemon supports these nicknames, and which ones, is to consult the crontab manpage.

See Also