[launchd] Jobs scheduled at midnight

Hello,


I have a question regarding how launchd handles tasks that are scheduled to start -- using StartCalendarInterval -- at a point in time where your computer is shutdown.


The documentation launchd.plist(5) clearly states:

"Unlike cron which skips job invocations when the computer is asleep, launchd will start the job the next time the computer wakes up."

Below is an example launchd job that executes when started manually.

It fails to execute if the computer was shut down (power off) at 22:00 and awakened at 06:00 the next morning.

The job is not disabled locally or globally.

plutil -p ~/Library/LaunchAgents/local.XX.plist
{
  "Program" => "/usr/local/bin/fab"
  "StandardOutPath" => "/var/log/XX.log"
  "ProcessType" => "Background"
  "StartCalendarInterval" => {
    "Minute" => 0
    "Hour" => 0
  }
  "ProgramArguments" => [
    0 => "fab"
    1 => "backup:dbname=XX"
  ]
  "StandardErrorPath" => "/var/log/XX.log"
  "WorkingDirectory" => "/Users/XX/XX-backup"
  "Label" => "local.XX"
}


1) Can I safely assume that shutdown is equivalent to asleep in the context of launchd?

2) Are there any known bugs associated with the StartCalendarInterval key?

3) Is my understanding correct that if a job is scheduled to start while the computer is shutdown, then the job will start when the computer becomes awake?

4) Given that the above example job executes normally when started manually, what might be preventing the job from starting in the morning (after returning from a shutdown state)?


Thank you for your time

Does it run by schedule if the computer is on and awake? (You might want to test with a more convenient starting time, of course.)

Ken,


Thanks for your reply.


Yes, the job does run on schedule when the computer is awake. I verified this by removing the "Hour" key.

I'll try including the RunAtLoad key to see if it solves the issue.


Once I find an answer to this issue, I'll document it and make an explanation available to others experiencing the same problem.


Hank

Accepted Answer

From the Daemons and Services Programming Guide


--------


If you schedule a launchd job by setting the StartCalendarInterval key and the computer is asleep when the job should have run, your job will run when the computer wakes up. However, if the machine is off when the job should have run, the job does not execute until the next designated time occurs.


All other launchd jobs are skipped when the computer is turned off or asleep; they will not run until the next designated time occurs.


Consequently, if the computer is always off at the job’s scheduled time, both cron jobs and launchd jobs never run. For example, if you always turn your computer off at night, a job scheduled to run at 1 A.M. will never be run.


--------


1) Can I safely assume that shutdown is equivalent to asleep in the context of launchd?


No, “asleep" and "turned off" are treated differently by launchd. If the computer is asleep and the StartCalendarInterval key does specify a run-time, then launchd will run I missed job after the computer wakes up. If the computer is turned off at the job scheduled (StartCalendarInterval) time, the job will not be run after the computer is powered on — unless other settings like RunAtLoad specify otherwise.


2) Are there any known bugs associated with the StartCalendarInterval key?


I never came across any known issues related to the StartCalendarInterval key.


3) Is my understanding correct that if a job is scheduled to start while the computer is shutdown, then the job will start when the computer becomes awake?


False — see #1


4) Given that the above example job executes normally when started manually, what might be preventing the job from starting in the morning (after returning from a shutdown state)?


See #1

[launchd] Jobs scheduled at midnight
 
 
Q