Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I created a launchd agent which opens a non-password-protected sparse disk image (and mounts its solitary volume) daily at 2:45AM in preparation for backup software to write to the volume. The launchd agent's plist file is saved in ~/Library/LaunchAgents/. Here are the plist file's parameters:

Label: my.DiskImageOpener
KeepAlive: NO
RunAtLoad: NO
StartCalendar:
    Hour: 2
    Minute: 45
ProgramArguments:
    hdiutil
    attach
    /path/to/MyDiskImage.sparseimage

As long as the computer's display is awake, the agent works as advertised, opening the disk image and mounting its volume at the time specified in the launch agent. On the other hand, if the display is asleep at the time specified in the launch agent, the disk image fails to open until the display is awoken from sleep (even if that is several hours later), at which point the disk image immediately opens and mounts its volume. The same phenomenon happens if I replace the hdiutil attach /path/to/MyDiskImage.sparseimage command with open /path/to/MyDiskImage.sparseimage, or place the command in a separate executable shell script that the launch agent executes. Also, even though the display is asleep when the above-described problem occurs, the computer itself is awake. Also, I tested having the launch agent execute a dummy executable shell script (echo $(date)) instead of hdiutil, and it executes normally even when the display is asleep.

So it seems that the problem occurs specifically when trying to open a disk image from a launchd launch agent while the computer's display is asleep. Why does the display's sleep vs awake status have any effect on opening a disk image? How can I circumvent this problem?

I am grateful for any help with this puzzling problem. (I am using a MacBook Pro laptop running OS X 10.8.2 Mountain Lion.)

share|improve this question
    
Have you tried to disable putting hard drives to sleep? Or using crontab: run EDITOR=nano crontab -e and add a line like 2 45 * * * hdiutil attach /path/to/MyDiskImage.sparseimage. –  ؘؘؘؘ Jan 21 '13 at 13:12
    
I must confess that I was hoping for a launchd rather than a crontab solution since Apple seems to be heading in that direction. In any case, I think I stumbled on the solution. As long as I keep the computer set to never sleep in the Energy Saver preference pane, then even if the display is asleep and/or the hard drive is set to sleep when possible, then the launch agent runs successfully and mounts the volume. Inexplicably, if the computer is set to any setting other than never to sleep (even though it may in fact be awake), the launch agent fails. Thank you for the suggestions. –  scolfax Jan 21 '13 at 15:47
    
@scolfax you might like to schedule your Mac to wake up at 2:44 using Energy Saver's Schedule… option. This would allow your Mac to continue saving energy at other times: dssw.co.uk/energysaver/#schedule –  Graham Miln Jul 14 '13 at 13:23

1 Answer 1

It is likely your MacBook is asleep when launchd is scheduled to run your disk mount command. launchd does not change its behaviour because of the state of the display.

Your launchd job will run so long as:

  • the Mac is awake
  • the launchd job is active
  • you are logged in (the job ticket is in ~/Library/)

Use pmset's debug logs to find out if your Mac was awake or asleep. The following command will print a log of everything pmset knows:

pmset -g everything

Be sure to check the system.log for messages related to your failed launchd job. If a problem is encountered launchd will log a message.

Applications > Utilities > Console.app > system.log

Mac OS X Energy Saver includes the ability to power on or wake your Mac at a predefined time. You might want to use this ability to wake your Mac a minute before your launchd job is scheduled.

System Preferences.app > Energy Saver > Schedule…
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.