cron 是如何判断@reboot状态执行

最近有个需求为开机的时候服务器自动执行一次puppet agent -t命令,如何利用puppet管理这一个动作呢,想到了以下三个方案:

  1. 写一个sysv风格的shell script放在/etc/init.d/目录下,然后用puppet的官方常用类型资源service来定制开机启动此脚本,譬如,脚本为/etc/init.d/runppagent.sh,puppet的语法如下
    file { '/etc/init.d/runppagent':
        ensure => 'present',
        owner  => 'root',
        group  => 'root',
        mode   => '0755',
        source => "puppet:///modules/项目代号/runppagent",
    }
    service { 'runppagent': #这里要注意的是此资源一定要在/etc/init.d/目录下
        enable => true,
    }
  1. 利用puppet统一管理文件/etc/rc.local文件,把puppet agent -t这一操作写在/etc/rc.local文件里面,开机的时候会执行这个文件的命令
  2. 利用puppet 的cron @reboot功能来实现,puppet语法如下
# 开机执行puppet agent -t操作
    cron { 'excute the puppet agent':
        ensure => present,
        command => '/usr/local/bin/puppet agent -t',
        user => 'root',
        special => 'reboot',
    }

经过考虑,第一个方式不太可靠,因为放在/etc/init.d下的脚本执行有一定的顺序,如果提前执行了puppet agent -t这个命令会出事的,so第一个方式去除;第二个方式,因为之前线上的/etc/rc.local文件有好几个版本,现在要统一管理起来的风险也挺大,so 也去除;那么就很开心的使用了第三张方式

重点就要说说第三种方式,有个特别的地方就是利用了cron的一个特性@reboot,来判断是否要执行这一操作。这时候我就有个疑问,cron是如何判断服务器是否reboot了? 我又想了下是否有以下可能

  • cron这个daemon重启了就认为服务器已经重启?
  • cron根据uptime信息来判断服务器是否重启?
  • 捕捉run level的状态来判断服务器是否重启?

作为一名it人员当然就是,写一个cron条目来测试上述条件是否成立,最后发现都不能触发@reboot这个操作,那么我们就来看看源代码吧,因为the code will tell u everything,(虽然自己写代码很菜

#ifdef DEBIAN
#define REBOOT_FILE "/var/run/crond.reboot"
    /* Run on actual reboot, rather than cron restart */
    if (access(REBOOT_FILE, F_OK) == 0) {
            /* File exists, return */
                log_it("CRON", getpid(),"INFO",
               "Skipping @reboot jobs -- not system startup");
            return;
    }
    /* Create the file */
    if ((rbfd = creat(REBOOT_FILE, S_IRUSR&S_IWUSR)) < 0) {
        /* Bad news, bail out */
            log_it("CRON",getpid(),"DEATH","Can't create reboot check file");
        exit(0);
    } else {
        close(rbfd);
        log_it("CRON", getpid(),"INFO", "Running @reboot jobs");
    }


        Debug(DMISC, ("[%d], Debian running reboot jobs\n",getpid()));

#endif
        Debug(DMISC, ("[%d], vixie running reboot jobs\n", getpid()));
    for (u = db->head;  u != NULL;  u = u->next) {
        for (e = u->crontab;  e != NULL;  e = e->next) {
            if (e->flags & WHEN_REBOOT) {
                job_add(e, u);
            }
        }
    }

这小段代码你能看出是依据REBOOT_FILE /var/run/crond.reboot来判断服务器是否重启,我们再看/var/run这个目录的官方说法,抽我

This directory contains system information data describing the system since it was booted. Files under this directory must be cleared (removed or truncated as appropriate) at the beginning of the boot process. Programs may have a subdirectory of /var/run

上面一寸英文的解释就是说,开机的时候这个目录下的文件都要被清除,因此再结合cron节选的c代码我们可以得知是根据/var/run/crond.reboot此文件的存在与否。

最后经过测试,先把文件/var/run/crond.reboot删除,再restart cron,则触发@reboot操作,有兴趣的朋友可以尝试一下。

题外话,debian到底是在哪个步骤来清除/var/run这个目录的的呢,首先我们可以看到/var/run这个目录,是链接到/run下面的,再用df命令来看到/run是一个tmpfs(临时文件系统)

这是debian手册给出的解释
After mounting all the filesystems, temporary files in "/tmp", "/var/lock", and "/var/run" are cleaned for each boot up.

root@cc-unknown23344:/etc/init.d# ls -l /var/run
lrwxrwxrwx 1 root root 4  4月 16  2013 /var/run -> /run

root@cc-unknown23344:/etc/init.d# df -h /run
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            26G  180K   26G   1% /run
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Puppet理论定义: Puppet 是一个跨平台的集中化配置管理系统,它使用自有的描述语言,可管理配置文件、用户...
    属于你的世界阅读 997评论 0 2
  • 1.puppet 是什么 puppet是一个开源的软件自动化配置和部署工具,很多大型IT公司均在使用puppet对...
    milo_e1ce阅读 4,938评论 0 4
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,566评论 9 467
  • Linux定时任务Crontab命令详解 linux 系统则是由 cron (crond) 这个系统服务来控制的。...
    孙燕超阅读 1,834评论 0 4
  • 一、项目目标 搭建一个高可用web集群网站 二、项目规划 2.1 ip地址规划 2.2 拓扑图 2.3 相关说明 ...
    夏日之光阅读 3,988评论 0 1