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 attacj /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.
How can I get the launch agent to open the disk image and mount its volume when the display is asleep?
I am using a MacBook Pro laptop running OS X 10.8.2 Mountain Lion. Thank you for any assistance with this problem.
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, opening the disk image and mounting 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.