How to Remotely Sleep a Mac from Anywhere with SSH or an iPhone

Mar 14, 2012 - 25 Comments

Remotely Sleep a Mac

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.

We’ll cover two methods to remotely put a Mac to sleep from anywhere. 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:
  • ssh username@127.0.0.1

  • 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:
  • tell application "System Events" to sleep
    Remote Sleep a Mac

  • 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

    Remote Sleep a Mac via Email

  • Click “OK” to add the new rule set and click “Apply” to apply the sleep ruleset to all inboxes

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 mac@osxdaily.com 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 modern versions of Mac OS and Mac OS X, though there is no technical limitation and they should function identically in earlier versions of Mac OS X as well.

.

Related articles:

Posted by: William Pearson in Mac OS, Tips & Tricks

25 Comments

» Comments RSS Feed

  1. John says:

    I have completed the apple script and it works perfectly on my iMac operating latest version of Yosemite. I now have a small well continuously running in my Menu Bar. The dropdown menu says Sleepmac.scpt Running. Is this normal?

  2. […] Read how to set up remote sleeping Macs via email here […]

  3. GMG says:

    Not sure if this is due to Mountain Lion, but I’m finding this excellent solution no longer works and have tried without success to find a solution.
    While the rule remains in the Preferences for Mail, next to button “Run AppleScript” is another button “No Scripts Installed”. I went into the finder and found the original script “sleepmac.scpt” and tried to reconnect it. The only option is “Open in Finder”, but it doesn’t allow me to choose this as the script. Annoyingly, I can open it, but not assign it to run. Have you found a fix?

  4. […] latter AppleScript is also used in our guide on how to remotely put a Mac to sleep using an iPhone or ssh. stLight.options({ […]

  5. […] an IR port, or if you’re not in the same room (let alone the same state), you can always sleep a Mac with email or using SSH after some minimal […]

  6. jch says:

    For SSH, you should use a public key and disable password access. Anything else on a computer of any kind connected to the Internet is asking for trouble.

  7. jch says:

    Considering how easy it is to forge a sender address for email, I wouldn’t recommend any kind of email triggered action like this.

    You would be seriously unimpressed if someone decided to send a sleep now messages a hundred times a day.

  8. Jindra Saur says:

    chittu: U can use program WakeOnLan. There is also dashboard gadget with the same name. Or (if you have any Linux machine) use command “wakeonlan -i your_proadcast_ip_address your_device_hardware_address”

  9. Jindra Saur says:

    I use “pmset sleepnow”

  10. Rune says:

    Great! tip Is there a fast way to wake up my Mac also? If I for example want to use logmein from work to control my sleeping home machine.

  11. Cameron says:

    Another option is “shutdown -s now”
    You have to be root to do it though. Thanks for the intro to the osascript command.

  12. chittu says:

    How to wake up over ssh?

  13. Gray says:

    Thank you, this was very useful and seems to be a fast method of triggering Sleep. I am sometimes on a different floor from my Mac and do not put it sleep because something is downloading.

  14. […]   Clicchiamo su OK quindi su Applica per impostare la regola alle caselle di posta elettronica selezionate (in caso di piu’ account sul sistema). Per verificare che tutto funzioni correttamente inviamo un’email utilizzando le impostazioni definite nella regola ma prima di farlo, salviamo il lavoro svolto!  Fonte – osXdaily  […]

  15. vdiv says:

    For crying out loud, don’t tell people to run sshd on their computer when they don’t necessarily know how to secure it. People need to learn how to setup a Firewall on the Mac first and secure their router.

    Why not just set the System Preferences: Energy Saver to put the system to sleep if idle after certain time?

  16. Brian G says:

    Great article!

    Much more simple, but you can also hold down the “Play/Pause” button on an Apple Remote Control and put a paired Mac to sleep that way. You need to be in the same room though :)

  17. Dr_Hicks says:

    Do you know if there is a tool to remote wipe a Mac ?

  18. Kris says:

    Better yet.. just download Mocha VNC on your iphone

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site