0

I run a Python script 4 times a day at what I'd like to be an exact, down to the second, time. I have been doing this for a while using launchd on my Mac and the script has been running at nearly the exact second I want. For example, I've set launchd to start running the script at exactly 6 am and the script generally starts running at 6 am plus 100 or 200 milliseconds; exactly what I want. However, for some unknown reason (perhaps a just-before-this-started-happening OS X update?), the script is now starting to run anywhere from a few seconds to more than a few seconds after 6 am, too late for the ultimate purpose of the script. I'm now thinking that a more robust way to make sure the main part of my script runs at exactly the second I want is to start the script sometime before 6 am, perhaps at 5:59 am, and then in the script itself, have it check the time every second (or more) and then when the exact second I want the rest of the script to run comes, the rest of the script will run. So, my question is, how do I run a loop, or whatever, in the script itself, which will then kick off the rest of the script at the exact (or nearly exact) moment, for example 6:00:00 am?


Okay, the question was marked as a duplicate but it still took a bit to figure out the best way to resolve my problem. Just in case anyone is looking for the answer here, here is what I did:

import datetime
import time
##### build in a timer to make sure the script starts to execute at the exact time and second, i.e. 6am, 10am, 2pm, and 6pm, PDT

secs = datetime.datetime.now().strftime("%S.%f")
secs = float(secs)
sleep_secs = 60 - secs
time.sleep(sleep_secs)

Then, still using launchd, I just set the scripts to start one minute before I really want them to run, so it doesn't matter if they start a few seconds late. The code I provide above makes sure that the main part of the script runs at exactly on the hour. In fact, in testing this, the script starts within a couple milliseconds of the hour, even better than before.

Jeff F
  • 975
  • 4
  • 14
  • 24
  • You might want to look into how to solve the `launchd` problem. Because any solution you hack up yourself isn't really going to be "more robust". You're almost sure to forget about a dozen edge cases Apple already thought of—and there will be some that don't have easy solutions, like making sure that the program is always running as long as the machine is on and knows how to deal with restoring its state if forcibly restarted and so on (although you can punt some those parts to launchd again…). – abarnert Apr 24 '15 at 01:45
  • Yes, thanks, I wish I knew where to begin on figuring out why the script has been running fine and at the exact time 4 times a day for nearly 3 months and now, after this latest update (but I'm not certain it started immediately after the update) I now have these few to more than a few seconds delay in the script starting. I just don't even have a clue where to begin on figuring that out. But thanks for your comment. – Jeff F Apr 24 '15 at 02:10
  • There are lots of places online where you can get help with that kind of system administration problem, from SuperUser to Apple's forums. – abarnert Apr 24 '15 at 07:54
  • Thanks @abarnert. Appreciate the info! – Jeff F Apr 24 '15 at 14:16

0 Answers0