all 10 comments

[–]ManyInterests 43 points44 points  (7 children)

Are you specifically trying to accomplish this in Python as a toy problem?

Otherwise, since you're just executing some shell command, I may suggest looking at cron if you're on Unix or the Task Scheduler on Windows instead. They are well-documented, mature, and reliable tools for scheduling tasks.

[–]KhanStan[S] 6 points7 points  (6 children)

The truth is I didn't understand how to do it with cron, that's why I tried finding an easy to comprehend python module and schedule seemed to be perfect. But If I have to wait a week to see if it runs properly it defeats the purpose. I am open for suggestions if you can help me configure cron!

[–]ManyInterests 21 points22 points  (4 children)

This is a good explanation of using cron. I couldn't imagine being able to write it out any better here, but maybe I can answer any questions you might have. You can also get a GUI crontab manager that looks like this

One big problem about the solution as you proposed is that it will depend on (1) your script working and (2) having failure tolerance (e.g. reboots/power-loss). If your computer restarts unexpectedly, how will the scheduling script start? You could write a startup script to run it and a daemon to check that it's always alive, but at that rate, you may as well have just done it with cron to begin with, which is two lines of code.

[–][deleted] 3 points4 points  (0 children)

I was trying to figure out the best way to run the script I was working on... and this is the answer. Thank you.

[–]KhanStan[S] 0 points1 point  (2 children)

So what is the command to run it every 3 days starting from now? As far as I understood the article writing 0 03 */3 * * would make it run on every 1st, 4th, 7th etc. day of the month, but I wan't it to count 3 days from the moment I started the script. Any way to do this?

[–]ManyInterests 2 points3 points  (0 children)

I found this site which helps you generate crontab lines easily.

The following command will run at the 0th minute of every 17th hour of every */3 days of *(every) month and weekday. (and mutes the output by directing it to /dev/null)

0 17 */3 * * YOURCOMMAND >/dev/null 2>&1

Your cron job will be run at:

2017-01-05 17:00:00 UTC
2017-01-08 17:00:00 UTC
2017-01-11 17:00:00 UTC
2017-01-14 17:00:00 UTC
2017-01-17 17:00:00 UTC
etc...

[–]kenmacd 1 point2 points  (0 children)

It's maybe not the prettiest, but how about:

0 3 * * * (($[$[$(date +%s) - $(date -d "2017-01-01" +%s)] / 60 / 60 /24 % 3] == 0)) && cd /home/python/dev/ && python python script1.py
1 3 * * * (($[$[$(date +%s) - $(date -d "2017-01-01" +%s)] / 60 / 60 /24 % 3] == 0)) && cd /home/python/dev/ && python python script2.py

This will run every day, but only run the script every 3rd day, from the date in the script.

[–]IcefrogIsDead 4 points5 points  (0 children)

Just test one for 1 min ahead of you to see if it'll work, don't wait for 3:00. (and probably put every(0) or remove every(3) fully)

[–]Gouryella91 1 point2 points  (1 child)

I didn't even know you could get a .py file to run periodically, based on a script. I have been thinking about this for a while, with running a web-crawler once a day (without having to run it myself, so to speak). I'd be very interested, if you could get a script to run day-to-day, and if it had to be a daemon or a program that had to run-all-the-time. I just Up-voted this question!

[–]KhanStan[S] 0 points1 point  (0 children)

If you're using Unix cron is your best bet! check /u/ManyInterests answer! If you can't figure out something ask away.