I have a MacBook Air. I often use Microsoft's RDP client to connect to a virtual workstation. I need this RDP session to be automatically closed (quit RDP) when I close the lid, before OS X goes to sleep.

How can this functionality be achieved?

  • What is the OS X application you're using to connect to the windows box? Are you using microsoft.com/mac/remote-desktop-client ?
    – Colyn1337
    Aug 22, 2013 at 14:19
  • Yes, Remote Desktop Client from Microsoft. Aug 22, 2013 at 14:20
  • You might want to ask this question at the apple.stackexchange.com site. Tho someone here may know, I'd envision needing to create a custom sleep script for this which checks for the RDP program and closes it. What you're trying to accomplish is the opposite of what Sleep was intended for.
    – Colyn1337
    Aug 22, 2013 at 14:24
  • @Colyn1337 OS X questions are on topic here. Please do not advise users to post their question elsewhere, as cross-posting is not allowed. Maxim can have the question migrated to Ask Different if it does not get a suitable answer within, say, a couple of days. Thanks for your understanding.
    – slhck
    Aug 22, 2013 at 14:25
  • @slhck I never said it wasn't. I was simply recommending a more specific community.
    – Colyn1337
    Aug 22, 2013 at 14:27

1 Answer 1


There are a couple of tools that allow you to intercept OS X' power status changes:

Sleep Watcher installation

Let's try this with Sleep Watcher. Download the tool from Bernhard Baehr's homepage and follow the installation instructions: assuming you unpacked the download to your Desktop, call the following commands from a Terminal window:

sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8
sudo cp ~/Desktop/sleepwatcher_2.2.1/sleepwatcher /usr/local/sbin
sudo cp ~/Desktop/sleepwatcher_2.2.1/sleepwatcher.8 /usr/local/share/man/man8

This will install the sleepwatcher binary and the manpages. You will be prompted for an administrator password.

Creating a sleep script

Now, let's create a sleep script called ~/.sleep. It will contain the commands that are run when the machine is put to sleep (i.e. when the lid closes).

touch ~/.sleep
open -e !$

In this file, add the following:

osascript -e 'tell application "Remote Desktop Connection" to quit'

This will quit the Microsoft RDP application through AppleScript commands. You can do this with any application as long as you change the name according to its title. Save the file. Now we need to make it executable:

chmod +x ~/.sleep

Testing the sleep script

Now test your script by first starting the RDP program, then running the following in Terminal:

/usr/local/sbin/sleepwatcher --verbose --sleep ~/.sleep

This will not output anything, so leave both RDP and the Sleep Watcher running, then close the lid of your MacBook. Wait a couple of seconds until it really goes to sleep. Then wake it up again. Did RDP close? Great! Did something go wrong? Look at the output of the sleepwatcher command to get a hint about where it failed.

You can now cancel the Sleep Watcher by pressing CtrlC in Terminal.

Running the script in background

In order to have the Sleep Watcher run at all times, you need to make it a LaunchDaemon. Create a new configuration file by copying the example configuration and rc-scripts.

sudo cp ~/Desktop/sleepwatcher_2.2.1/config/de.bernhard-baehr.sleepwatcher-20compatibility.plist /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist
sudo cp ~/Desktop/sleepwatcher_2.2.1/config/rc.* /etc

Now load the configuration files with launchd:

sudo launchctl load /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist

That's all you need. Now the .sleep file will always be executed whenever you sleep your Mac.

  • Since the time this marvelous answer was provided, Microsoft has changed the name of it's application, so the correct osascript call is now this: osascript -e 'tell application "Remote Desktop Connection" to quit' Aug 8, 2016 at 13:49
  • @MaximV.Pavlov Thanks for the comment, but isn't this what my answer already says?
    – slhck
    Aug 10, 2016 at 18:25
  • My bad, probably I had something different, and you initially povided a correct naming. I am now calling two osascripts by the way. One to close Microsoft Remote Desktop, and the other one to close actual session windows which go by the name "Remote Desktop Connection". Aug 11, 2016 at 19:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .