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:
pmset sleepnow
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.
alias 0=”pmset sleepnow”
I’ve always preferred:
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
Then I make to make an automator workflow into a service so it can be invoked from anywhere, tell it to run that command, and save a keyboard shortcut to call it. The end result on my system:
I can press cmd+opt+ctrl+L from anywhere and it does the same thing as if I choose “Login Window…” from the fast user switching menu, sort of like the Win+L screnlock that’s built into Windows. Clean, quick, reliable and secure.
Here’s a guy who’s outlined the general process I followed. It’s the last of several solutions he describes.
https://osxdaily.com/2012/03/30/lock-mac-desktop-command-line/
You can also use “option+command+eject”
Where can I find this wallpaper? Thanks.
Apple menu > Sleep…
Isn’t that faster?
Pretty tough to “Apple menu > Sleep…” if you’re logged in remotely via ssh.
Probably you are right.
But personally I have absolutely no interest to log via ssh…
I don’t even know what it means :D
Sounds like an apple user.
Don’t worry I am sure apple will remove the scary terminal soon enough.
Terminal should not be removed from the system because it can be very useful.
But I think that the “normal Apple user” is a person that doesn’t want to complicate the own existence with terminal or similar things.
The use of the terminal is completely contrary to the Apple’s philosophy.
Typical Windows users don’t used the Command Prompt, Powershell or RDP either. Face it, PCs have largely become appliances. Most users of every operating system just want to run their apps. They don’t want/need a deep understanding of the underpinnings.
But don’t worry, their is a place for powerusers on every OS.
To: Rob
Yeah I don’t care what typical windows user do I clump them in with typical apple users.
The terminal is not just to understand how the underpinnings of the OS works. For me I use the terminal to SSH into remote machines to run batch process on more powerful machines, basic file management (cp and mv are far better than the finder), raw conversion of files from my DSLR (with dcraw), etc… There is no reason to use a more or less untested GUI program when there are terminal programs that do the exact same thing and have been tested for decades.
I am not a system admin or anything like that! I use terminal programs because they are normally better! Not sure why it is part of apple culture that the terminal is only for configuring your computer. Hell vim in the terminal is far better than any GUI text editor that run well in OS X.
Do you need to set up a root account to use “sudo shutdown -s now” or do you just have to be logged in as an admin?
sudo executes whatever command is following as root (admin) so you don’t need to setup another account, you’ll just need the admin password when using it.
Will, thanks for the info. A few days before this post, I was playing around in “the scary terminal” (I’m looking at you, Bob). I was logged in under my admin account and goofing around. I remembered an old unix command from my Novell days so I tried entering “shutdown -h now”, which from what I vaguely remembered would power down the whole server immediately. Terminal kept looking for some higher level password from me, which I could not provide because I had not enabled root. After reading this thread, I realized i didnt type the sudo prefix. Duh. So, tried the command again, with sudo prefix this time, and hey, the mac shutdown instantly.
Cool! Thanks again. This has sparked an interest in getting to know cli a bit better. sudo. wow.
Hey guys how did you make the menu bar like that? Thanks in advance :)
Black menu bar is accomplished with MenuBarFilter
https://osxdaily.com/2011/09/23/dark-menu-bar-mac-os-x/
I actually have that app but i don’t get it work like that… I mean i’ll love to have the menu bar like in the picture (Blue-Black) Thanks for your answer :)
What is the theme being used in the picture?
you can also use:
“sudo shutdown -s now”
:)
You also have “sudo shutdown -s now”