我有以下 .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。
1 回答

青春有我
2. 在 Python 中,堅持使用
TA貢獻1784條經驗 獲得超8個贊
問題
訪問每個 crontab 作業的 cron 表達式(計劃)。
解決方案
有多種解決方案,具體取決于您接下來要使用 cron 表達式執行的操作。
1. 在 Python 之外,使用grep
crontab -l | grep 'bar'
結果會是這樣的:
0?10?*?*?*?bash?bar.sh 0?23?*?*?*?bash?bar.sh
2. 在 Python 中,堅持使用python-crontab
包
eachJob
2.1迭代searchResultsList時?可以打?。?/p>
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 如果你想單獨使用 'python-crontab' 訪問調度規則,你可以這樣做:
for?eachJob?in?searchResultsList: ????print(eachJob[0],?eachJob[1],?eachJob[2],?eachJob[3],?eachJob[4])
結果會是這樣的:
0?10?*?*?* 0?23?*?*?*
添加回答
舉報
0/150
提交
取消