I use my Macbook Pro at the office and at home and at each location I have network storage that I want to have easily accessible. And since I only put my laptop to sleep and not turning it off, having that storage automatically available is an even bigger pain.
Thankfully a gentleman created SleepWatcher. This allows you to execute scripts when your laptop goes to sleep and wakes up. The program runs as a service and requires a bit of command-line experience to install. Here is a quick walk-through of a script that runs when my laptop wakes up and attempts to mount my samba shares.
Download Sleepwatcher to your Desktop and extract it.
1. Install the SleepWatcher software
$ sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8 $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8 |
2. Create your mount script at ~/wakeup.scpt. I used Applescript, but you can use any interpreted language.
#!/usr/bin/osascript try mount volume "smb://username@server/share$/" end try |
3. Create your Launcher profile at ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>de.bernhard-baehr.sleepwatcher</string> <key>ProgramArguments</key> <array> <string>/usr/local/sbin/sleepwatcher</string> <string>-V</string> <string>-w /usr/local/sbin/wakeup_mounts.scpt</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> </dict> </plist> |
4. Load the launchd agent configuration, this starts the SleepWatcher daemon:
$ sudo launchctl load ~/Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist |
The script will now execute when the laptop wakes from a sleep state.