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 Mac 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 how you can initiate sleep on any Mac by using the command line.
How to Put a Mac to Sleep from Command Line of Mac OS X with pmset
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 and the following command syntax:
Hit return and the Mac is instantly put to sleep.
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.
How to Put Mac to Sleep from Command Line with AppleScript
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.