代码之家  ›  专栏  ›  技术社区  ›  NeoVe

解析crontab文件,并在下次计划的作业将以类人的形式执行时打印

  •  1
  • NeoVe  · 技术社区  · 7 年前

    我有这个脚本,以前有一些问题,但无论如何,现在它正常工作。

    import croniter
    import datetime
    import re
    
    now = datetime.datetime.now()
    
    def main():
    
        f = open("/etc/crontab","r")
        f1 = f.readlines()
        for x in f1:
            if not re.match('^[0-9*]', x):
                continue
            a = re.split(r'\s+', x)
            cron = croniter.croniter(' '.join(a[:5]), now)
            cron.get_next(datetime.datetime)
            print(x)
    
    if __name__ == "__main__":
        main()
    

    这会打印我的 crontab 文件如下:

    17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
    
    25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
    
    47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
    
    52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
    

    没关系,但我用的是 croniter 出于某种原因,我可以通过简单地执行以下操作来实现此结果:

        def main():
    
            f = open("/etc/crontab","r")
            f1 = f.readlines()
            for x in f1:
                print(x)
    
        if __name__ == "__main__":
            main()
    

    我要找的是,以一种类似人类的方式,像 this answer

    0 回复  |  直到 7 年前