Puppet cron资源介绍(二十七)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Puppet cron资源介绍(二十七)相关的知识,希望对你有一定的参考价值。
cron资源
主要用来管理操作系统的定时任务(即crontab),之前文章也写过一个cron模块举例,计划任务并非都要使用cron资源,linux下只要将一个文件放置/var/spool/cron目录下其实crontab就会执行,之前的文章也是这样写的.
cron资源属性:
cron { ‘resource title‘: name => # (namevar) The symbolic name of the cron job. This name is ensure => # The basic property that the resource should be... command => # The command to execute in the cron job. The... environment => # Any environment settings associated with this... hour => # The hour at which to run the cron job. Optional; minute => # The minute at which to run the cron job... month => # The month of the year. Optional; if specified... monthday => # The day of the month on which to run the... provider => # The specific backend to use for this `cron... special => # A special value such as ‘reboot‘ or ‘annually‘... target => # The name of the crontab file in which the cron... user => # The user who owns the cron job. This user must... weekday => # The weekday on which to run the command... # ...plus any applicable metaparameters. }
参数注释:
command:crontab要执行的命令,由于环境变量的问题,建议调用命令时使用绝对路径,或指定cron资源的environment属性.
ensure:指定该资源是否启用,可设置present值表示启用,设置absent值表示关闭.
environment:在crontab环境里指定环境变量,如PATH=/bin:/usr/bin:/usr/sbin.也可以通过:导入更多环境变量.
hour:运行crontab的小时,可设置成0-23,单位是小时.
minute:运行crontab的分钟,可设置为0-59,单位是分钟.
month:设置crontab运行的月份,可设置成1-12,单位是月.
monthday:一个月中的哪一天,可设置成1-31,单位是日.
weekday:运行crontab的星期数,可设置为0-7,单位是天.
name:crontab的注释,注释用于帮助管理员区分不同的crontab.
provider:默认是系统自带的crontab程序,通常不需要指定此参数值,puppet会默认匹配系统自带的定时管理任务程序.
user:将crontab加入某一个系统账号中,默认是加入执行守护进程的系统账户中.
示例一:
定义crontab计划任务同步ntpdate服务器时间.
定义ntpdate类,做计划任务.
class cron::ntpdate { cron {"ntpdate": ensure => present, command => ‘/usr/sbin/ntpdate 1.cn.pool.ntp.org‘, user => ‘root‘, minute => ‘*/5‘, } }
node节点中添加此计划任务.
node /sh-(proxy|web)\d+/ inherits base { case $::hostname { /sh-proxy\d+/: { include nginx } "sh-web1": { include haproxy include cron::ntpdate } } }
客户端同步ntpdate.
[[email protected] haproxy]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version ‘1508433121‘ Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: /Stage[main]/Cron::Ntpdate/Cron[ntpdate]/ensure: created Notice: Finished catalog run in 0.43 seconds
查看计划任务.
[[email protected] haproxy]# crontab -l # HEADER: This file was autogenerated at Fri Oct 20 01:12:02 +0800 2017 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: ntpdate */5 * * * * /usr/sbin/ntpdate 1.cn.pool.ntp.org
示例二:
做一个ping计划任务,每天的2,4点执行ping,注意使用"[]".
node /sh-(proxy|web)\d+/ inherits base { case $::hostname { /sh-proxy\d+/: { include nginx } "sh-web1": { include haproxy include cron::ntpdate include cron::ping } } }
class cron::ping { cron {"ping": command => ‘ping -c1 www.baidu.com 2>&1 >> /dev/null‘, user => ‘root‘, hour => [2, 4], } }
客户端执行:
[[email protected] haproxy]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version ‘1508433611‘ Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: /Stage[main]/Cron::Ping/Cron[ping]/ensure: created Notice: Finished catalog run in 0.42 seconds
crontab计划任务查看:
[[email protected] haproxy]# crontab -l # HEADER: This file was autogenerated at Thu Oct 19 17:19:52 +0800 2017 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: ntpdate */5 * * * * /usr/sbin/ntpdate 1.cn.pool.ntp.org # Puppet Name: ping * 2,4 * * * ping -c1 www.baidu.com 2>&1 >> /dev/null
示例三:
执行ping计划任务,每天2-4之间,每隔10分钟执行一次.
class cron::ping { cron {"ping": command => ‘ping -c1 www.baidu.com 2>&1 >> /dev/null‘, user => ‘root‘, hour => [‘2-4‘], minute => ‘*/10‘, } }
客户端更新:
[[email protected] haproxy]# puppet agent -t Notice: Ignoring --listen on onetime run Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-web1.localdomain Info: Applying configuration version ‘1508433856‘ Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: /Stage[main]/Cron::Ping/Cron[ping]/minute: defined ‘minute‘ as ‘*/10‘ Notice: /Stage[main]/Cron::Ping/Cron[ping]/hour: hour changed ‘2,4‘ to ‘2-4‘ Notice: Finished catalog run in 0.36 seconds
计划任务查看.
[[email protected] haproxy]# crontab -l # HEADER: This file was autogenerated at Thu Oct 19 17:23:56 +0800 2017 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: ntpdate */5 * * * * /usr/sbin/ntpdate 1.cn.pool.ntp.org # Puppet Name: ping */10 2-4 * * * ping -c1 www.baidu.com 2>&1 >> /dev/null
本文出自 “青衫解衣” 博客,请务必保留此出处http://215687833.blog.51cto.com/6724358/1975231
以上是关于Puppet cron资源介绍(二十七)的主要内容,如果未能解决你的问题,请参考以下文章