Scheduling Python script using Python CronTab on Windows 7

风流意气都作罢 提交于 2021-02-09 02:54:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!