What I’m trying to do is execute a script at a certain time, and for the most part it does work when I run it a short time frame from execution, say around 10 – 15 minutes. So I would run the timer script and 10 – 15 minutes later it executes it’s command.
Here is the code I’m using:
import time
import myscript
from sys import exit
while 1:
if time.strftime("%H") == "7" and time.strftime("%M") == "15":
myscript.main()
exit()
What I want is for the script to execute everyday in the morning. When I get to my computer later in the day, I can see that the script is stuck on this process.
I’ve tried this on two machines, an Ubuntu 12.04 64 bit box and a Windows XP box with cygwin installed and they both show this same issue. When I leave the computers I’m only locking them, not putting them on standby. I’ve tried running the script and locking it, and then short unlocking it, but I can see that that isn’t the issue because the script runs fine.
Also to note, when I to my computer, it seems to be running very laggy and this script seems to be taking an awful lot of CPU usage, in the range of 30 – 50%.
What else am I missing, or is this not the ideal way to go about it?
Having no sleep in there explains why your system freaks out. It is spinning as fast as your cpu can go.
If you are just experimenting with a wrapping script that will run a function at a specific time, you can just do this:
But here is a big problem to think about. What happens if
foo()takes a long time or hangs from its own operation?What might have been happening in your case is that your sleep (being set to 60 seconds) might have really missed that minute window. You would have to sleep for smaller increments to make sure you check the minute multiple times.
Aside from this, use a cron job. It is meant to do scheduled tasks. cron will exist in the cygwin shell that you mentioned you are using anyways. Refer here for setting up a crontab on windows