I recently tried to switch to launched on my Mac Mini (10.7.5) from cron on a PC. I searched and think I have things set up right, but it seems that the scheduled script starts significantly after the requested time. Details at https://stackoverflow.com/questions/32829026/using-launched-and-plist-starts-late. Thanks for guidance or experiments to try.
3 Answers
Since your per-user launch agent does actually execute, albeit later than scheduled, it is highly likely that your system may be asleep at the scheduled time. I would recommend reading through the man page by running the following command from a terminal session:
$ man 5 launchd.plist
Regarding the StartInterval
option specifically, the man page has this to say:
Unlike cron which skips job invocations when the computer is asleep, launchd will start the job the next time the computer wakes up. If multiple intervals transpire before the computer is woken, those events will be coalesced into one event upon wake from sleep.
If you are working remotely, or not physically present at the scheduled time — which is not clear from your question — a good starting point would be to confirm that the system has not entered sleep mode prior to the scheduled time of your launch agent.
-
Interesting. Thanks. Checking my System Preferences -> Energy Saver settings, I go into Sleep within 15 minutes, so I'm definitely "sleep"ing when the job should start up, and the "put hard disks to sleep" checked also. So I should not request the computer to be "sleep"ing at the time that I'm expecting the job to start up? I would have liked to just be able to leave the computer on constantly, and just do the recording every week. Any way to wake up just before needed? Or just fire up the computer less than the sleep time before the desired job start time? Or just don't sleep? Thanks.– gkd720Nov 9, 2015 at 0:34
-
You could try scheduling a repeating 'Start up or wake' event in System Preferences>Energy Saver>Schedule just prior to the scheduled launch agent is due to execute. Nov 9, 2015 at 0:45
Note that the documentation for StartInterval
has changed as of 10.10, see https://forums.developer.apple.com/thread/23361 for a thread documenting this change.
The new text reads
StartInterval
This optional key causes the job to be started every N seconds. If the system is asleep during the time of the next scheduled interval firing, that interval will be missed due to shortcomings in kqueue(3). If the job is running during an interval firing, that interval firing will likewise be missed.
Apparently this has always been 'broken' and the documentation has been updated to reflect that.
So the answer is that StartInterval
doesn't handle sleep, and you can either prevent sleep (as you found) or switch to another method to trigger your job. For instance StartCalendarInterval
does coalesce events after returning from sleep, so that might be a good option.
OK, I turned off "sleep"ing and the scheduled job starts and ends as expected. My next step is to run with the full 2 hour job run time. I had experimented with the jobs runnings 2 minutes each as a test, and they did. So next step is the real job, but I expect it to work. I'll report when it does. Thanks for the suggestion/solution.
OK, that was it. It was "sleep"ing whenever a job was to start. Not letting it sleep now allows all jobs to run as expected. Thanks.
-
I'm glad that helped. Could you mark the answer as correct in that case? It may be useful for others with similar problems. Nov 23, 2015 at 8:16