Sleep a Mac from the Command Line

Jul 22, 2012 - 20 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. To try this yourself, launch Terminal and use one of the following commands, remember there is no warning, sleep is immediate:

pmset sleepnow

That is one of the most simple uses of pmset, which is a full featured power management utility.

You can also use AppleScript to instantly initiate sleep from Terminal:

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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

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

20 Comments

» Comments RSS Feed

  1. Tomgi says:

    You also have “sudo shutdown -s now”

  2. Mathias says:

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

  3. John C. says:

    What is the theme being used in the picture?

  4. Xavier says:

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

  5. 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.

  6. 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.

  7. playmakerx says:

    Where can I find this wallpaper? Thanks.

  8. Mahdi says:

    You can also use “option+command+eject”

  9. 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.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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