在 Windows 7 上使用 Python CronTab 调度 Python 脚本



我想在Windows平台上使用python-crontab模块调度一个python脚本。找到以下代码片段解决方法,但很难配置。脚本名称cronTest.py

from crontab import CronTab
file_cron = CronTab(tabfile='filename.tab')
mem_cron = CronTab(tab="""
* * * * * command
""")

例如,假设我想使用以下名为 dateTime.py 的脚本打印每 5 分钟的日期和时间:

import datetime
with open('dateInfo.txt','a') as outFile:
    outFile.write('n' + str(datetime.datetime.now()))

如何执行dateTime.py并设置每 5 分钟到 cronTest.py 的 cron 作业。

您是否运行了嵌入式调度程序?请参阅文档中Running the Scheduler部分:

tab = CronTab(tabfile='MyScripts.tab')
for result in tab.run_scheduler():
    print "This was printed to stdout by the process."
由于 Windows 没有 crontab 进程,

因此您必须将 crontab 馈送到现有守护程序中,或者在进程中使用此run_scheduler为自己创建守护进程。

最新更新