How to Remotely Sleep a Mac from Anywhere with SSH or an iPhone
Ever been away from your Mac and wished you could put it to sleep remotely? Maybe you accidentally left a Mac on at home or at work, or maybe you just left a Mac running so a download could complete. You don’t have to leave it running while you’re gone though, here are two different ways you can remotely sleep a Mac. The first method uses SSH and thus requires Terminal access, and the other uses just email which allows you to sleep a Mac with an iPhone or iPad just by sending a message from a verified email address.
Remotely Sleep a Mac with SSH
The first method assumes knowledge of SSH and the Terminal and is more advanced than the email method mentioned below. You will need to enable SSH server on the target Mac beforehand for this to work, this can be done quickly through System Preferences > Network > Enable Remote Login. Make note of the Macs IP address too, that is what you’ll connect to it with.
- Use Terminal and SSH to the target Mac, be sure to specify the appropriate user name and IP address:
- Once logged in, type the following command:
osascript -e 'tell application "System Events" to sleep'
There is no warning or hesitation, the target Mac immediately goes to sleep and the SSH connection will die as a result. The main downside to this method is you’ll need access to an SSH client to activate the sleep method, these are widely available for Mac OS X (Terminal), Windows (PuTTY), and iOS (Prompt or MobileTerminal). If you don’t want to have to use SSH to sleep the Mac, you can also use put Macs to sleep remotely by sending an email, it takes a little bit more to set up though.
Remotely Sleep a Mac with an iPhone via eMail
This may be the best method because you can put the Mac to sleep anytime by shooting off an email from an iPhone (or iPad 3G/4G). You will need to always have Mail.app running on the target Mac for this to work:
- Open AppleScript Editor (/Applications/Utilities/)
- Create a new AppleScript containing the following exactly:
- Save the AppleScript as “sleepmac.scpt” and put it in your Documents folder
- Open Mail app, pull down Mail menu, and select “Preferences”
- Click on “Rules” and choose “Add Rule”
- Name the description something like “Sleep Mac” and create new conditions with the following options:
- if: ALL
- From – Contains – (specify verified email address here)
- Subject – Is equal to – “Sleep now”
- Perform following actions: Run AppleScript – ~/Documents/sleepmac.scpt
- Click “OK” to add the new rule set and click “Apply” to apply the sleep ruleset to all inboxes
tell application "System Events" to sleep
Verify everything is working by sending an email from the address you specified with the subject “Sleep now”, the Mac should sleep immediately. If it doesn’t work, verify the AppleScript was set up properly, and that the Rule set is configured properly and for the recipient inbox that Mail.app is configured for. With the ruleset shown in the screenshot above, any email sent from firstname.lastname@example.org with the subject “Sleep now” will sleep the target Mac immediately.
Both SSH and sleep through Mail have been tested to work on Macs running OS X 10.7 Lion and OS X 10.8 Mountain Lion, though there is no technical limitation and they should function identically in earlier versions of Mac OS X as well.