4

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?

slhck
  • 223,558
  • 70
  • 607
  • 592
Maxim V. Pavlov
  • 1,583
  • 6
  • 22
  • 35
  • What is the OS X application you're using to connect to the windows box? Are you using http://www.microsoft.com/mac/remote-desktop-client ? – Colyn1337 Aug 22 '13 at 14:19
  • Yes, Remote Desktop Client from Microsoft. – Maxim V. Pavlov Aug 22 '13 at 14:20
  • You *might* want to ask this question at the http://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 '13 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 '13 at 14:25
  • @slhck I never said it wasn't. I was simply recommending a more specific community. – Colyn1337 Aug 22 '13 at 14:27
  • Why specifically does it need to disconnect before sleeping? The server will automatically disconnect it when it detects the connection has dropped, and the client will re-establish the connection when your mac wakes up. – Darth Android Aug 22 '13 at 14:27
  • @Colyn1337 Just keep in mind that while that's a more specific community, there are also less people there; Also, communities themselves are not segregated-- I'm sure many people browse both Ask Different and SuperUser, if not the majority of them. – Darth Android Aug 22 '13 at 14:29
  • @DartAndroid, the situation is that I am not sure who will be around the laptop when I wakes up from sleep. What if it's the people who will be willing to find something that is really on a remote RDP-access machine. With RDP dropped on lid action, I am sure in such case people won't even know I was using RDP at first glance. I know this may sound strange, but that is a real world scenario I need to be prepared for. – Maxim V. Pavlov Aug 22 '13 at 15:08

1 Answers1

10

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.

slhck
  • 223,558
  • 70
  • 607
  • 592
  • 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'` – Maxim V. Pavlov Aug 08 '16 at 13:49
  • @MaximV.Pavlov Thanks for the comment, but isn't this what my answer already says? – slhck Aug 10 '16 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". – Maxim V. Pavlov Aug 11 '16 at 19:05