Sleep a Mac from the Command Line
Sleep can be invoked instantly on any Mac through the command line by running the pmset command or a very simple AppleScript run in OS X. This can be useful for many reasons, whether scripting, system administration, remote management with SSH, or perhaps you just live in the command line.
We’ll show you two ways you can initiate sleep on any Mac by using the command line.
How to Put a Mac to Sleep from Command Line of OS X
To try this yourself, launch Terminal and use one of the following commands. Remember there is no warning, sleep is immediate.
The first trick uses pmset:
That is one of the most simple uses of pmset, which is a full featured power management utility.
As mentioned, sleep happens immediately, so if you aren’t ready for that you may want to apply a wait, or use a variation of the next trick since AppleScript can be scheduled easily.
Using AppleScript from the command line is another way to instantly initiate sleep from Terminal. The syntax for the AppleScript sleep method is as follows:
osascript -e 'tell application "Finder" to sleep'
osascript is a command line tool that runs OSA scripts, the -e flag executes the script in quotes rather than looking for a file, and the text in quotations is basic AppleScript.
Using either method should override anything else running in Mac OS X and force the system to sleep. You can also target the application “System Events” if you do encounter something preventing sleep:
osascript -e 'tell application "System Events" to sleep'
The latter AppleScript is also used in our guide on how to remotely put a Mac to sleep using an iPhone or ssh.