问题
I want to schedule a python script using the python-crontab module on Windows platform. Found the following snippet to work around but having a hard time to configure. Script name cronTest.py
:
from crontab import CronTab
file_cron = CronTab(tabfile='filename.tab')
mem_cron = CronTab(tab="""
* * * * * command
""")
Let's say, for example, I want to print date & time for ever 5 mins using the following script, named dateTime.py
:
import datetime
with open('dateInfo.txt','a') as outFile:
outFile.write('\n' + str(datetime.datetime.now()))
How do I execute dateTime.py
and setup the cron job for every 5mins through cronTest.py
.
回答1:
Did you run the embedded scheduler? See Running the Scheduler
section in the documentation:
tab = CronTab(tabfile='MyScripts.tab')
for result in tab.run_scheduler():
print "This was printed to stdout by the process."
Because windows doesn't have a crontab process, you have to either feed your crontabs into an existing daemon or use this run_scheduler within your process to create a daemon for yourself.
来源:https://stackoverflow.com/questions/48311909/scheduling-python-script-using-python-crontab-on-windows-7