5

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.

4
  • Did you try putting */5 * * * * <PATH_TO_PYTHON> <PATH_TO_dateTime.py> in place of * * * * * command and running cronTest.py again? Commented Jan 18, 2018 at 1:39
  • I tried replacing command with path to my script it didn't work, I guess I need to know what exactly I should pass to tab in cron = CronTab(tab=""" */5 * * * * python <path to script>""") my_cron=cron.new(command='python D:\Pyhon\currentDate.py') my_cron.minute.every(1) Commented Jan 18, 2018 at 2:41
  • You might have to specify the complete path to python in your crontab. Commented Jan 18, 2018 at 4:17
  • I tried but it didn't work Commented Jan 18, 2018 at 5:40

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand one thing: What should be the "MyScripts.tab", in the problem presented by @g1devops it will be the file dateTime.py ?
Windows does have a scheduler, however - in fact, since Windows 95. I was sort of hoping to find a cross platform python scheduler, which is how I ended up here. Oh well.
If you know how it works, and how python could interact with it, perhaps it's a feature that could be added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.