Search code examples
cronpuppet

Creating crontab task in puppet using a cron expression


I'm trying to create a crontab task using Puppet. Problem is puppet is asking you to set parameters like "hour", "minute", "month", etc, to define at which moment the tasks have to be executed. I can't find a parameter using a cron expression, for example "*/5 * * * *" or "15 6 * * 1". Is there any way to do that?


Solution

  • Here is what I finally wrote:

    #In my parameters, so I can pass the array using Foreman, for example
    Array $cron_jobs = [['58 7 * * *', '/eloi/ksh/eloi-batch/scripts/purgeRepertoireLogTrace.ksh >/dev/null 2>&1', 'purgeRepertoireLogTrace'], ['*/5 * * * *',
      '/eloi/ksh/eloi-batch/scripts/replicationReferentielFichier.ksh >/dev/null 2>&1', 'replicationReferentielFichier']],
    
    ...
    
    $cron_jobs.each |$cron_job| {
        $cron_expr = split($cron_job[0], ' ')
    
        cron{ "cron-${cron_job[2]}":
          command     => "${cron_job[1]}",
          minute      => "${cron_expr[0]}",
          hour        => "${cron_expr[1]}",
          monthday    => "${cron_expr[2]}",
          month       => "${cron_expr[3]}",
          weekday     => "${cron_expr[4]}",
          user        => "${appuser[0]}",
          environment => "PATH=/bin:/usr/bin:/usr/sbin",
        }
      }
    

    Using the split function, I'm able to convert the cron expression from the table into a parameters for a Puppet cron resource.

    I could put the whole cron string in a single dimension array, but some of my commands contain spaces, so it wouldn't work properly. It also gives me the opportunity to add another field to build the resource name.