如何解决如何遍历所有crontab作业并打印作业的日程表,命令和注释 1在Python之外,使用grep 2在Python中,坚持使用python-crontab包
我有以下.py代码
from crontab import CronTab
...
searchResultsList = cron.find_command('bar')
for eachJob in searchResultsList:
print("Found this job",eachJob.command,eachJob.comment)
当我眼中的crontab -l
时,我可以看到.py正在找到它应该做的工作,但是如何获取.py程序来访问计划规则(* / 10 2-4 0 4 * )的每个工作?像eachJob.rules
或eachJob.schedule
之类的东西。
解决方法
问题
访问每个crontab作业的cron表达式(时间表)。
解决方案
根据接下来要对cron表达式执行的操作,有多种解决方案。
1。在Python之外,使用grep
crontab -l | grep 'bar'
结果将是这样的:
0 10 * * * bash bar.sh
0 23 * * * bash bar.sh
2。在Python中,坚持使用python-crontab
包
2.1遍历searchResultsList时,可以打印eachJob
:
for eachJob in searchResultsList:
print("Found this job",eachJob)
结果将是这样的:
Found this job 0 10 * * * curl -X GET https://maker.ifttt.com/trigger/event/with/key
Found this job 0 23 * * * curl -X GET https://maker.ifttt.com/trigger/event/with/key
2.2根据Get attribute from existing cron (#72) · Issues · Martin Owens / python-crontab · GitLab,如果您想单独使用“ python-crontab”访问调度规则,则可以执行以下操作:
for eachJob in searchResultsList:
print(eachJob[0],eachJob[1],eachJob[2],eachJob[3],eachJob[4])
结果将是这样的:
0 10 * * *
0 23 * * *
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。