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.

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.

share|improve this question

5 Answers 5

up vote 7 down vote accepted

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; I do not know about scripts triggered on sleep.

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

share|improve this answer
1  
Scenario seems to do what I'm looking for. Thanks! –  sh-beta Oct 10 '11 at 1:23
    
I had Scenario fire the applescript which fires off a bash script and voilá. –  redolent Sep 30 '13 at 21:46
    
To end an application just type this in a new AppleScrip window and save it in the SleepScripts folder: tell application "CodeBox" quit end tell –  brainray Jun 24 at 19:56

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

share|improve this answer
    
Here are a bunch of Mac OSX Hints that use SleepWatcher. –  studgeek Nov 8 '12 at 16:17

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.

share|improve this answer
1  
Very cool sounding utility, its overkill for just this but if you have a need to trigger on more than wake/sleep then it sounds pretty cool. I like the Growl support also so you know what it is doing. And its free :). –  studgeek Nov 8 '12 at 16:22
    
Should be the accepted answer because its free –  Kevin Mar 15 at 3:03

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'.

share|improve this answer

Another program which you might discover and be tempted to try is EventScripts. Although it is a very impressive program which I would recommend in general, after testing it for this purpose it does NOT actually appear to be able to trigger scripts quickly enough for them to run before your Mac goes to sleep, and instead the sleep script will not run until after the Mac has woken up, which (obviously) defeats the purpose.

I hope to save you and anyone else reading this the hours of testing it took me to come to this conclusion. I'm sorry to have to say that it failed, however, because in so many respects it is a really nicely designed and quite powerful and flexible program. It's also quite inexpensive ($3). The low-level performance just doesn't seem to be there, however, for running a script before sleeping.

On the other hand, in my tests (so far) sleepwatcher has appeared to be quite reliable and fast. It's also free, which is awesome. I'll keep testing and if I find it to not work sometimes I'll update my answer, and keep testing other options.

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.