我想在 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
并通过 cronTest.py
设置每 5 分钟的 cron 作业。
最佳答案
您是否运行了嵌入式调度程序?请参阅 the documentation 中的运行调度程序
部分:
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 为您自己创建一个守护进程。
关于python - 在 Windows 7 上使用 Python CronTab 调度 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48311909/