使用 cron 表达式在 puppet 中创建 crontab 任务
Creating crontab task in puppet using a cron expression
我正在尝试使用 Puppet 创建一个 crontab 任务。问题是 puppet 要求您设置“小时”、“分钟”、“月”等参数,以定义必须在哪个时刻执行任务。我找不到使用 cron 表达式的参数,例如“*/5 * * * *”或“15 6 * * 1”。有什么办法吗?
通配符(*)只需要将参数留空即可,比如我创建的;
class scratch::crontest {
cron { 'logrotate1':
command => '/usr/sbin/logrotate --version',
user => 'root',
hour => '*/5',
}
cron { 'logrotate2':
command => '/usr/sbin/logrotate --version',
user => 'root',
hour => '15',
minute => '6',
}
}
我将其应用于一个节点并创建了该节点;
[root] # crontab -l
# HEADER: This file was autogenerated at 2022-04-20 16:53:05 +0000 by puppet.
# HEADER: While it can still be managed manually, it is definitely not recommended.
# HEADER: Note particularly that the comments starting with 'Puppet Name' should
# HEADER: not be deleted, as doing so could cause duplicate cron jobs.
# Puppet Name: logrotate1
* */5 * * * /usr/sbin/logrotate --version
# Puppet Name: logrotate2
6 15 * * * /usr/sbin/logrotate --version
关于此的 Puppet 文档似乎仅适用于 Puppet 5.5,但如果您使用的是 Puppet 服务器或具有代理的节点,您可以 运行 puppet describe cron
查看文档你的版本。
这是我最后写的:
#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",
}
}
使用 split 函数,我能够将 table 中的 cron 表达式转换为 Puppet cron 资源的参数。
我可以将整个 cron 字符串放在一维数组中,但我的某些命令包含空格,因此无法正常工作。它还让我有机会添加另一个字段来构建资源名称。
我正在尝试使用 Puppet 创建一个 crontab 任务。问题是 puppet 要求您设置“小时”、“分钟”、“月”等参数,以定义必须在哪个时刻执行任务。我找不到使用 cron 表达式的参数,例如“*/5 * * * *”或“15 6 * * 1”。有什么办法吗?
通配符(*)只需要将参数留空即可,比如我创建的;
class scratch::crontest {
cron { 'logrotate1':
command => '/usr/sbin/logrotate --version',
user => 'root',
hour => '*/5',
}
cron { 'logrotate2':
command => '/usr/sbin/logrotate --version',
user => 'root',
hour => '15',
minute => '6',
}
}
我将其应用于一个节点并创建了该节点;
[root] # crontab -l
# HEADER: This file was autogenerated at 2022-04-20 16:53:05 +0000 by puppet.
# HEADER: While it can still be managed manually, it is definitely not recommended.
# HEADER: Note particularly that the comments starting with 'Puppet Name' should
# HEADER: not be deleted, as doing so could cause duplicate cron jobs.
# Puppet Name: logrotate1
* */5 * * * /usr/sbin/logrotate --version
# Puppet Name: logrotate2
6 15 * * * /usr/sbin/logrotate --version
关于此的 Puppet 文档似乎仅适用于 Puppet 5.5,但如果您使用的是 Puppet 服务器或具有代理的节点,您可以 运行 puppet describe cron
查看文档你的版本。
这是我最后写的:
#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",
}
}
使用 split 函数,我能够将 table 中的 cron 表达式转换为 Puppet cron 资源的参数。
我可以将整个 cron 字符串放在一维数组中,但我的某些命令包含空格,因此无法正常工作。它还让我有机会添加另一个字段来构建资源名称。