设为首页 收藏本站
查看: 1186|回复: 0

[经验分享] Puppet cron资源介绍(二十七)

[复制链接]

尚未签到

发表于 2017-10-31 16:20:04 | 显示全部楼层 |阅读模式
cron资源
        主要用来管理操作系统的定时任务(即crontab),之前文章也写过一个cron模块举例,计划任务并非都要使用cron资源,linux下只要将一个文件放置/var/spool/cron目录下其实crontab就会执行,之前的文章也是这样写的.

cron资源属性:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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类,做计划任务.
1
2
3
4
5
6
7
8
class cron::ntpdate {
    cron {"ntpdate":
        ensure => present,
        command => '/usr/sbin/ntpdate 1.cn.pool.ntp.org',
        user => 'root',
        minute => '*/5',
    }
}




node节点中添加此计划任务.
1
2
3
4
5
6
7
8
9
10
11
node /sh-(proxy|web)\d+/  inherits base {
    case $::hostname {
        /sh-proxy\d+/: {
             include nginx
          }
         "sh-web1": {
            include haproxy
            include cron::ntpdate
            }
        }
}




客户端同步ntpdate.
1
2
3
4
5
6
7
8
9
10
[iyunv@sh-web1 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





查看计划任务.
1
2
3
4
5
6
7
[iyunv@sh-web1 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,注意使用"[]".
1
2
3
4
5
6
7
8
9
10
11
12
node /sh-(proxy|web)\d+/  inherits base {
    case $::hostname {
        /sh-proxy\d+/: {
             include nginx
          }
         "sh-web1": {
            include haproxy
            include cron::ntpdate
            include cron::ping
        }
    }
}




1
2
3
4
5
6
7
class cron::ping {
    cron {"ping":
        command => 'ping -c1 www.baidu.com 2>&1 >> /dev/null',
        user    => 'root',
        hour    => [2, 4],
    }
}





客户端执行:
1
2
3
4
5
6
7
8
9
10
[iyunv@sh-web1 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计划任务查看:
1
2
3
4
5
6
7
8
9
[iyunv@sh-web1 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分钟执行一次.
1
2
3
4
5
6
7
8
class cron::ping {
    cron {"ping":
        command => 'ping -c1 www.baidu.com 2>&1 >> /dev/null',
        user    => 'root',
        hour    => ['2-4'],
        minute  => '*/10',
    }
}




客户端更新:
1
2
3
4
5
6
7
8
9
10
11
[iyunv@sh-web1 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





计划任务查看.
1
2
3
4
5
6
7
8
9
[iyunv@sh-web1 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






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-406346-1-1.html 上篇帖子: Puppet exec资源介绍(二十六) 下篇帖子: Puppet notify资源参数(二十八)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表