How can I automatically kill sshfs on sleep? [duplicate]

MacBook Pro 2010 running OS X Lion.

Is it possible to run scripts on sleep and wake events?

My specific application is that I've got Dropbox-synced Truecrypt volumes that I regularly use on both my MacBook and iMac. I rarely shut down my MBP since closing the lid is much faster and easier, but this means that I have to remember to dismount my Truecrypt volumes and let them sync back up before closing my MBP. It's trivial enough to script mounting/dismounting of them, but I'd like to go a step further and have these scripts run automatically.


Solution 1:

It appears that as the OS ships, sleep/wake cannot trigger scripts, but third party solutions have been developed.

  • Scenario appears to be one such solution.
  • Power Manager also seems to add some script triggering abilities. I can see it can trigger scripts on wake from sleep and it can trigger scripts on sleep. Power Manager - trigger on power on screen shot

I have no personal experience with these programs or their vendors.

Solution 2:

SleepWatcher may be of use.

From the description: It can be used to execute a Unix command when the Mac or the display of the Mac goes to sleep mode or wakes up, after a given time without user interaction or when the user resumes activity after a break or when the power supply of a Mac notebook is attached or detached. It also can send the Mac to sleep mode or retrieve the time since last user activity.

It's working fine for me on 10.6.8. Various versions are available to support 10.1(!) thru 10.7

Solution 3:

This is my own app, so consider that, but ControlPlane has the ability to do actions based on Sleep/Wake. You'll find it at http://www.controlplaneapp.com/.

ControlPlane is a fork of MarcoPolo and has been updated to run on Snow Leopard and Lion.

Solution 4:

I developed the following simple Launch Daemon to provide a 'wake' trigger for scripts on Leopard:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>wake-alert</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>-c</string>
        <string>z=/tmp/wake; test -s $z || { date > $z; say w; }; 
tail -1 /Library/Logs/DirectoryService/DirectoryService.server.log | grep -v Sleep || > $z
        </string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Library/Logs/DirectoryService/DirectoryService.server.log</string>
    </array>
    <key>ExitTimeOut</key>
    <integer>2</integer>
    <key>ThrottleInterval</key>
    <integer>1</integer>
</dict>
</plist>

You can replace 'say w' with your code. I write a date to z but you could change this to write anything. You may need to increase 'ExitTimeOut' for some scripts. My Launch Daemon resides at /Library/LaunchDaemons/wake-alert.plist

The Launch Daemon watches DirectoryService.server.log and writes to a temporary file provided the file is empty. The file is cleared by a log 'Sleep' entry and by Shut Down.

If you use 'fast user switching' to sleep you will find that scripts which require a logged in user will attempt to run too early and fail unless you modify the Launch Daemon - perhaps grep 'Succeeded' in 'secure.log'.

Solution 5:

Was interested in that question previously but didn't find any step by step guide on how to achieve it. Solution is based on open-source tool called sleepwatcher and apple launchd Was inspired by this article

So enjoy! :

  1. Install sleepwatcher => brew install sleepwatcher
  2. At first create => ~/.wakeup and ~/.sleep scripts and put some shell logic into it
  3. Add necessary permissions => chmod 700 ~/.sleep && chmod 700 ~/.wakeup
  4. Test it locally => /usr/local/sbin/sleepwatcher --verbose --sleep ~/.sleep - go to sleep with running terminal
  5. If it works then load process as launchd agent for all users => /Library/LaunchAgents
  6. Add symlink for sleepwatcher launchd task => sudo ln -sfv /usr/local/Cellar/sleepwatcher/2.2/de.bernhard-baehr.sleepwatcher-20compatibility.plist /Library/LaunchAgents/
  7. Load launchd task => sudo launchctl load /Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility.plist

Hope it will help anybody :)