1. 程式人生 > >Puppet cron資源介紹(二十七)

Puppet cron資源介紹(二十七)

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.

[root@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


查看計劃任務.

[root@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,註意使用"[]".

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],
    }
}


客戶端執行:

[root@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計劃任務查看:

[root@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分鐘執行一次.

class cron::ping {
    cron {"ping":
        command => ‘ping -c1 www.baidu.com 2>&1 >> /dev/null‘,
        user    => ‘root‘,
        hour    => [‘2-4‘],
        minute  => ‘*/10‘,
    }
}

客戶端更新:

[root@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


計劃任務查看.

[root@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



本文出自 “青衫解衣” 博客,請務必保留此出處http://215687833.blog.51cto.com/6724358/1975231

Puppet cron資源介紹(二十七)