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

g1d*_*ops 5 python windows cron scheduled-tasks python-3.x

我想在 Windows 平台上使用 python-crontab 模块安排一个 python 脚本。发现以下代码片段可以解决,但配置起来很困难。脚本名称cronTest.py

from crontab import CronTab
file_cron = CronTab(tabfile='filename.tab')
mem_cron = CronTab(tab="""
* * * * * command
""")
Run Code Online (Sandbox Code Playgroud)

举例来说,我想使用以下名为 的脚本打印 5 分钟的日期和时间dateTime.py

import datetime
with open('dateInfo.txt','a') as outFile:
    outFile.write('\n' + str(datetime.datetime.now()))
Run Code Online (Sandbox Code Playgroud)

如何dateTime.py每 5 分钟执行一次并设置 cron 作业cronTest.py

Mar*_*mo- 4

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

tab = CronTab(tabfile='MyScripts.tab')
for result in tab.run_scheduler():
    print "This was printed to stdout by the process."
Run Code Online (Sandbox Code Playgroud)

因为 Windows 没有 crontab 进程,所以您必须将 crontab 提供给现有的守护进程,或者在进程中使用此 run_scheduler 为自己创建一个守护进程。