August 16, 2021

Cron Job Management with Puppet Enterprise

How to & Use Cases
Infrastructure Automation

Puppet Enterprise can help manage your cron jobs and regularly scheduled tasks to ensure they aren't forgotten. Here's how it works.

Table of Contents:

What is a Cron Job?

A cron job is a recurring task that is scheduled at specific intervals using cron, a time-based job scheduler in Unix-like operating systems. It's useful for automating repetitive tasks like sending bulk emails, system maintenance, database cleanup, and more.

The Challenge of Cron Job Management

Many IT organizations use cron to run regularly scheduled tasks. The trouble is, it’s easy to forget what cron jobs exist where, and why. Forgotten cron jobs can wreak havoc on systems, and it's often difficult to track down the root cause: the forgotten cron task itself.

Puppet Enterprise includes a way to manage cron resources so you can see all the cron jobs under management from a central place, and know exactly what they do and when they do it.

Want to Do Even More with Puppet?


How to Use Puppet Automation for Cron Job Management

In this example, instead of creating a new cron job or scheduled task, we will take one that we know is correct and make sure that it is the same across all our systems.

1. Check Existing Cron Jobs and Scheduled Tasks

Cron jobs

puppet resource cron

Scheduled tasks on Windows

puppet resource scheduled_task

On one of my database servers I get:

cron { 'unmanaged:su_-mysqlcheck_--databases_webportal_customer-1':
  ensure  => 'present',
  command => 'mysqlcheck --databases webportal_customer',
  hour    => ['0'],
  target  => 'root',
  user    => 'root',

This represents a cron job I created manually that verifies my customer database from my web portal application, every night at midnight.

2. Create a Role and Profile

I'm going to add this to my Puppet code so that all the database servers for my web portal application run the same cleanup at the same time. To do that, I will first create a role and profile for this to live in.

Now at this point, it might be tempting to create a profile called profile::cron_jobs and put all our cron jobs in there. However, we are going to create one called profile::webportal_database instead. This is because it makes more sense to group our Puppet resources by the particular component they are relevant to than by their type. Using the knowledge we acquired in Task 1, our profile will look like this:

# site/profile/manifests/webportal_database.pp
class profile::webportal_database {
  # This code is copy-pasted from the `puppet resource cron` command
  cron { 'vacuum_customer_database':
    ensure  => 'present',
    command => 'mysqlcheck --databases webportal_customer',
    hour    => ['0'],
    target  => 'root',
    user    => 'root',

Note that I have changed the title of this resource to be more descriptive. This is just for readability.

Now that I have created a profile, I can create a role for my web portal database server:

# site/role/manifests/webportal_database
class role::webportal_database {
  include profile::base               # From Task 1
  include profile::webportal_database # The one we just created

3. Deploy Your Code

Now deploy your code (the same as when we set up the Puppet server).

4. Assign the New Role to Your Database Servers

We will assign our new role to our database servers by going into the Puppet Enterprise console and clicking Nodes > Classification > Add Group… This will allow us to add a group for our database servers and assign our new role to it.

Create a group called Database Servers. Click the newly created group from the list, and under Pin specific nodes to the group, open it up, add your database servers, and click Pin. Now go to the Classes tab and add your new role. If it is not available yet, you may need to click the Refresh button in the middle right of the screen. Once you have added the role::webportal_database role, click Commit Changes.

Run Puppet on your database servers using puppet agent -t or by clicking Run Puppet in the console.

Ready to try Puppet Enterprise for task automation? Get started with a free trial today!

Start My Trial

This blog was originally published on August 16, 2016 and has since been updated for accuracy and relevance.