August 16, 2021

Cron Job Management: How to Keep Scheduled Tasks Under Control

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.

Back to top

What is a Cron Job?

A cron job is a recurring task that is scheduled at specific intervals. Cron jobs automate tasks like sending bulk emails, system maintenance, database cleanup, security scans, fetching updates, file system integrity checks, and more.

While the underlying mechanisms, syntax, functions, and configurations differ between operating systems, automated scheduled tasks exist on both *nix OSes and Windows.

On *nix OSes, cron jobs are scheduled using cron, a time-based job scheduler utility, and configurations for cron jobs are stored in crontab files.

On Windows, cron jobs are called scheduled tasks and they're done using Task Scheduler (go figure). Task Scheduler features a GUI for managing cron jobs at specific times or intervals, or in reaction to a specific trigger.

  • (For some triggers, Task Scheduler might not have the minute-level granularity of cron jobs run with cron on *nix, but they’re largely comparable for scheduling tasks.)
Back to top

Cron Job Management Challenges

IT ops teams use cron jobs and scheduled tasks to make operations smoother. But as systems grow, the sheer scale of scheduled tasks can undo the operational efficiency they’re supposed to enable. Here’s why.

Documentation, Documentation, Documentation

Documentation is hard even with a small number of cron jobs, but it’s crucial, especially at scale. Keeping comprehensive documentation is essential to effective cron job management, maintenance, continuous ops, and knowledge transfer. Your team should be tracking the purposes and dependencies of cron jobs, as well as configurations, schedules, commands, and troubleshooting steps for each job.

Setting Up a Lot of Cron Jobs Gets Pretty Hairy

Cron job management is about more than just making sure they run when they’re supposed to. There are a lot of variables – multiple systems, multiple timezones, specialized tasks, and more complex jobs – and ballooning cron jobs can eat up resources during peak usage times.

Cron Jobs Get Lost in the Mix

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.

💪 Get the Puppet skills you need to tackle (almost) anything:


Back to top

How to Use Puppet Automation for Cron Job Management

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.

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.

Back to top

Automate Cron Job Management with a Puppet Enterprise Trial

Puppet Enterprise can help you automate tasks like these, including code deployment, to save you time and effort. 

If you're not using Puppet Enterprise for task automation, do yourself a favor and 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.

Back to top