Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 10 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. Power Manager - trigger on power on screen shot

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

share|improve this answer
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 '14 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
You can install SleepWatcher by Homebrew, e.g. brew install sleepwatcher. And see this post, it's very helpful. The SleepWatcher utility also supports OS X El Capitan (10.11). – Rockallite Jul 14 at 2: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

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

share|improve this answer
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 '14 at 3:03
can this used for mounting and unmounting drives with sleep and wake ? do we need to write automator scripts ? – bicepjai Dec 23 '15 at 3:18

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" "">
<plist version="1.0">
        <string>z=/tmp/wake; test -s $z || { date > $z; say w; }; 
tail -1 /Library/Logs/DirectoryService/DirectoryService.server.log | grep -v Sleep || > $z

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.

UPDATE I have not found sleepwatcher to be consistently reliable since originally writing this answer, although it initially appeared to be working. In my case I don't need to run arbitrary scripts, but instead just eject external drives. I've found a small and inexpensive app that does this very well so far: Jettison. This has very quickly become one of a small handful of utilities I can't live without. If your needs are the same as mine, I think you'll find it well worth the few bucks they charge.

share|improve this answer

Your Answer


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.