Skip to content

mskarademir/puppet-cron

Repository files navigation

Puppet Cron Module

master branch status

Notes

This module manages cronjobs by placing a file in /etc/cron.d.
It is a detached fork of torrancew/puppet-cron which seems to be abandoned.
It is backwards compatible with it and can be used as a drop-in-replacement.
This fork is Puppet 4 / future parser compatible.

It defines the following types:

  • cron::job - basic job resource
  • cron::job::multiple - basic job resource for multiple jobs per file
  • cron::hourly - wrapper for hourly jobs
  • cron::daily - wrapper for daily jobs
  • cron::weekly - wrapper for weekly jobs
  • cron::monthly - wrapper for monthly jobs

Additionally there is the cron class which can be used to install the correct cron package.

Installation

As usual use puppet module install rmueller-cron to install it.

Usage

The title of the job (e.g. cron::job { 'title':) is completely arbitrary. However, there can only be one cron job by that name.
The file in /etc/cron.d/ will be created with the $title as the file name.
Keep that in mind when choosing the name to avoid overwriting existing system cronjobs and use characters that don't cause problems when used in filenames.

cron

This module can optionally install the cron package if needed.
Most systems ship with cron already installed, doing this is usually not required. But you can use it via:

include cron

or:

class { 'cron': }

It allows specifiying the following parameter:

  • package_ensure - optional - defaults to "installed"

cron::job

cron::job creates generic jobs in /etc/cron.d. It allows specifying the following parameters:

  • ensure - optional - defaults to "present"
  • command - required - the command to execute
  • minute - optional - defaults to "*"
  • hour - optional - defaults to "*"
  • date - optional - defaults to "*"
  • month - optional - defaults to "*"
  • weekday - optional - defaults to "*"
  • user - optional - defaults to "root"
  • environment - optional - defaults to ""
  • mode - optional - defaults to "0644"

Example:
This would create the file /etc/cron.d/mysqlbackup and run the command mysqldump -u root mydb as root at 2:40 AM every day:

cron::job { 'mysqlbackup':
  minute      => '40',
  hour        => '2',
  date        => '*',
  month       => '*',
  weekday     => '*',
  user        => 'root',
  command     => 'mysqldump -u root mydb',
  environment => [ 'MAILTO=root', 'PATH="/usr/bin:/bin"', ],
}

Or define it using YAML:

---
cron::job:
  'mysqlbackup':
    command: 'mysqldump -u root mydb'
    minute: 0
    hour: 0
    date: '*'
    month: '*'
    weekday: '*'
    user: root
    environment:
      - 'MAILTO=root'
      - 'PATH="/usr/bin:/bin"'

cron::job::multiple

cron:job::multiple creates a file in /etc/cron.d with multiple cron jobs configured in it.
It allows specifiying the following parameters:

  • ensure - optional - defaults to "present"
  • jobs - required - a hash of multiple cron jobs using a similar structure as cron::job-parameters
  • environment - optional - defaults to ""
  • mode - optional - defaults to "0644"

And parameters of the jobs hash are:

  • command - required - the command to execute
  • minute - optional - defaults to "*"
  • hour - optional - defaults to "*"
  • date - optional - defaults to "*"
  • month - optional - defaults to "*"
  • weekday - optional - defaults to "*"
  • user - optional - defaults to "root"

Example:

cron::job::multiple { 'test_cron_job_multiple':
  jobs => [
    {
      minute      => '55',
      hour        => '5',
      date        => '*',
      month       => '*',
      weekday     => '*',
      user        => 'rmueller',
      command     => '/usr/bin/uname',
    },
    {
      command     => '/usr/bin/sleep 1',
    },
  ],
  environment => [ 'PATH="/usr/sbin:/usr/bin:/sbin:/bin"' ],
}

YAML definition:

---
cron::job::multiple:
  'test_cron_job_multiple':
    jobs:
      - {
          minute: 55,
          hour: 5,
          date: '*',
          month: '*',
          weekday: '*',
          user: rmueller,
          command: '/usr/bin/uname',
        }
      - {
          command: '/usr/bin/sleep 1',
        }

    environment:
      - 'PATH="/usr/sbin:/usr/bin:/sbin:/bin"'

That will generate the file /etc/cron.d/test with essentially this content:

PATH="/usr/sbin:/usr/bin:/sbin:/bin"

55 5 * * *  rmueller  /usr/bin/uname
* * * * *  root  /usr/bin/sleep 1

cron::hourly

cron::hourly creates jobs in /etc/cron.d that run once per hour. It allows specifying the following parameters:

  • ensure - optional - defaults to "present"
  • command - required - the command to execute
  • minute - optional - defaults to "0"
  • user - optional - defaults to "root"
  • environment - optional - defaults to ""
  • mode - optional - defaults to "0644"

Example:
This would create the file /etc/cron.d/mysqlbackup_hourly and run the command mysqldump -u root mydb as root on the 20th minute of every hour:

cron::hourly { 'mysqlbackup_hourly':
  minute      => '20',
  user        => 'root',
  command     => 'mysqldump -u root mydb',
  environment => [ 'MAILTO=root', 'PATH="/usr/bin:/bin"', ],
}

YAML definition:

---
cron::hourly:
  'mysqlbackup_hourly':
    minute: 20
    user: root
    command: 'mysqldump -u root mydb'
    environment:
      - 'MAILTO=root'
      - 'PATH="/usr/bin:/bin"'

cron::daily

cron::daily creates jobs in /etc/cron.d that run once per day. It allows specifying the following parameters:

  • ensure - optional - defaults to "present"
  • command - required - the command to execute
  • minute - optional - defaults to "0"
  • hour - optional - defaults to "0"
  • user - optional - defaults to "root"
  • environment - optional - defaults to ""
  • mode - optional - defaults to "0644"

Example:
This would create the file /etc/cron.d/mysqlbackup_daily and run the command mysqldump -u root mydb as root at 2:40 AM every day, like the above generic example:

cron::daily { 'mysqlbackup_daily':
  minute  => '40',
  hour    => '2',
  user    => 'root',
  command => 'mysqldump -u root mydb',
}

YAML definition:

---
cron::daily:
  'mysqlbackup_daily':
    minute: 40
    hour: 2
    user: root
    command: 'mysqldump -u root mydb'

cron::weekly

cron::weekly creates jobs in /etc/cron.d that run once per week. It allows specifying the following parameters:

  • ensure - optional - defaults to "present"
  • command - required - the command to execute
  • minute - optional - defaults to "0"
  • hour - optional - defaults to "0"
  • weekday - optional - defaults to "0"
  • user - optional - defaults to "root"
  • environment - optional - defaults to ""
  • mode - optional - defaults to "0644"

Example:
This would create the file /etc/cron.d/mysqlbackup_weekly and run the command mysqldump -u root mydb as root at 4:40 AM every Sunday, like the above generic example:

cron::weekly { 'mysqlbackup_weekly':
  minute  => '40',
  hour    => '4',
  weekday => '0',
  user    => 'root',
  command => 'mysqldump -u root mydb',
}

YAML definition:

---
cron::weekly:
  'mysqlbackup_weekly':
    minute: 40
    hour: 4
    weekday: 0
    user: root
    command: 'mysqldump -u root mydb'

cron::monthly

cron::monthly creates jobs in /etc/cron.d that run once per month. It allows specifying the following parameters:

  • ensure - optional - defaults to "present"
  • command - required - the command to execute
  • minute - optional - defaults to "0"
  • hour - optional - defaults to "0"
  • date - optional - defaults to "1"
  • user - optional - defaults to "root"
  • environment - optional - defaults to ""
  • mode - optional - defaults to "0644"

Example:
This would create the file /etc/cron.d/mysqlbackup_monthly and run the command mysqldump -u root mydb as root at 3:40 AM the 1st of every month, like the above generic example:

cron::monthly { 'mysqlbackup_monthly':
  minute  => '40',
  hour    => '3',
  date    => '1',
  user    => 'root',
  command => 'mysqldump -u root mydb',
}

YAML definition:

---
cron::monthly:
  'mysqlbackup_monthly':
    minute: 40
    hour: 3
    date: 1
    user: root
    command: 'mysqldump -u root mydb'

Contributors

  • Kevin Goess (@kgoess) - Environment variable support + fixes
  • Andy Shinn (@andyshinn) - RedHat derivatives package name fix
  • Chris Weyl (@RsrchBoy) - Fixed Puppet 3.2 deprecation warnings
  • Mathew Archibald (@mattyindustries) - Fixed file ownership issues
  • The Community - Continued improvement of this module via bugs and patches

About

Puppet module to manage cron jobs via /etc/cron.d

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 52.2%
  • Puppet 41.7%
  • HTML 6.1%