Sleep a Mac from the Command Line

Jul 22, 2012 - 21 Comments

Sleep a Mac from 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.

.

Related articles:

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

21 Comments

» Comments RSS Feed

  1. vadim says:

    alias 0=”pmset sleepnow”

  2. digitalb0y says:

    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.

  3. Mahdi says:

    You can also use “option+command+eject”

  4. playmakerx says:

    Where can I find this wallpaper? Thanks.

  5. Alberto I says:

    Apple menu > Sleep…

    Isn’t that faster?

    • BB.13 says:

      Pretty tough to “Apple menu > Sleep…” if you’re logged in remotely via ssh.

      • Alberto I says:

        Probably you are right.
        But personally I have absolutely no interest to log via ssh…
        I don’t even know what it means :D

        • Bob says:

          Sounds like an apple user.

          Don’t worry I am sure apple will remove the scary terminal soon enough.

          • Alberto I says:

            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.

          • Rob Bates says:

            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.

          • Bob says:

            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.

  6. Patrick says:

    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?

    • Will says:

      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.

      • Patrick says:

        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.

  7. Xavier says:

    Hey guys how did you make the menu bar like that? Thanks in advance :)

  8. John C. says:

    What is the theme being used in the picture?

  9. Mathias says:

    you can also use:
    “sudo shutdown -s now”
    :)

  10. Tomgi says:

    You also have “sudo shutdown -s now”

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