keepcalculator

06 Numbers and Powers

Sleep a Mac from the Command Line

Card No. 06·15891 · Updated · Standard formula, shown below

Yes, you can put a Mac to sleep directly from the command line using the built-in pmset command. Simply open Terminal and run sudo pmset sleepnow to instantly trigger system sleep, just as if you chose Apple Menu > Sleep. This works on all modern macOS versions and requires administrator privileges.

Why Use the Command Line to Sleep?

Most Mac users click the Apple menu or press a keyboard shortcut to sleep their computer. The command line offers several advantages:

  • Scripting: You can combine sleep with other commands in a shell script, for example, to save work, close apps, then sleep.
  • Remote access: If you SSH into your Mac from another device, you can put it to sleep without physical access.
  • Automation: Schedule sleep with launchd or cron jobs, or trigger it from other programs.
  • Precision: You can force sleep even if an app is preventing normal sleep (though you risk losing unsaved work).

The command is part of pmset, a tool that manages power management settings on macOS. It has been available since OS X 10.4 Tiger and remains in current versions.

How to Use pmset sleepnow

The basic command is:

sudo pmset sleepnow

You must prefix with sudo because sleepnow requires root privileges. When you run it, macOS will ask for your password (if you have admin rights). After entering it, the Mac sleeps immediately.

If you want to avoid typing your password every time, you can use osascript with an AppleScript that does not require sudo:

osascript -e 'tell application "System Events" to sleep'

This uses the System Events framework and works without admin rights, but it may be slower and can be blocked by accessibility permissions on some macOS versions.

What Does “Sleep” Actually Mean?

When your Mac sleeps, it enters a low-power state where the CPU, display, and most hardware are powered down, but RAM remains active to preserve your open documents and running apps. The Mac wakes quickly when you open the lid or press a key.

There are two other power states you might encounter:

  • Safe Sleep (hibernate): The Mac copies RAM contents to the hard drive before sleeping, offering extra data safety if the battery dies. This is controlled by the hibernatemode setting.
  • Shutdown: The Mac powers off completely; all unsaved work is lost.

The sleepnow command triggers the current default sleep mode, which is usually safe sleep on laptops and regular sleep on desktops.

Other Useful pmset Commands

Besides sleepnow, pmset can manage power settings:

  • pmset -g custom – Display current power management settings.
  • pmset -g assertions – Show which apps are preventing sleep (helpful for debugging).
  • sudo pmset -a displaysleep 10 – Set display sleep timeout to 10 minutes.
  • sudo pmset -a sleep 0 – Disable system sleep (0 means never).
  • sudo pmset -a hibernatemode 25 – Force safe sleep mode.

These commands let you configure sleep behavior from the command line, which is handy for scripting or remote management.

Common Problems and Solutions

Sometimes pmset sleepnow doesn’t work as expected. Here are common issues:

  • Password prompt not appearing: If you run sudo in a non-interactive shell (like a script), it may fail. Use sudo -n to check if you have cached credentials, or run the command manually once to cache them.
  • Mac wakes immediately: This can happen due to Power Nap, network activity, or a USB device. Check pmset -g to see if tcpkeepalive or powernap is enabled. You can disable them with sudo pmset -a powernap 0.
  • Command not found: pmset is in /usr/bin/, which is in the default PATH. If you get an error, use the full path: /usr/bin/pmset.
  • Sleep doesn’t work when plugged in: Some Macs have different settings for battery vs. AC power. Check both with pmset -g.

Alternatives to pmset sleepnow

If pmset doesn’t suit your needs, there are other command-line methods:

  • shutdown -s now – This puts the Mac to sleep (not shutdown) on some older macOS versions, but it’s deprecated. It works on macOS 10.15 and earlier; on newer versions, it may not work.
  • osascript -e 'tell application "System Events" to sleep' – Uses AppleScript, no sudo required, but may be slower.
  • caffeinate -u -t 1 – This simulates user activity for 1 second, which can trigger sleep if the system is set to sleep after a short idle time, but it’s not a direct sleep command.

For most users, pmset sleepnow is the most reliable and direct method.

Safety and Precautions

Sleeping your Mac from the command line is generally safe, but consider these points:

  • Save your work: Sleep preserves RAM, but if the battery dies or a crash occurs, unsaved work can be lost. Always save important documents before sleeping.
  • Remote sessions: If you SSH into a Mac and put it to sleep, the SSH connection will drop. You’ll need physical access or Wake-on-LAN to reconnect.
  • Admin rights: sudo requires an administrator account. If you’re a standard user, you can’t use pmset sleepnow directly.
  • No data corruption: Sleep is a normal operation; it doesn’t risk data corruption like a forced shutdown might.

Frequently Asked Questions

Does pmset sleepnow work on all Macs?

Yes, pmset is built into macOS on all Mac models, including Intel and Apple Silicon. The command works the same regardless of hardware.

Can I use pmset sleepnow without sudo?

No, sleepnow requires root privileges. If you don’t want to use sudo, try the AppleScript method with osascript.

How do I wake a Mac from sleep via command line?

You can’t wake it with a command because it’s asleep. You can use Wake-on-LAN (WOL) to send a network packet, but that requires setup and a second device. Physically opening the lid or pressing a key is the simplest.

What’s the difference between sleep and display sleep?

Display sleep turns off the screen but keeps the system running. System sleep (what sleepnow does) puts the whole Mac into a low-power state. You can set display sleep separately with pmset displaysleep.

Can I schedule a Mac to sleep using the command line?

Yes, you can use pmset schedule sleep to set a one-time or recurring sleep schedule. For example: sudo pmset schedule sleep "03/15/2024 22:00:00". You can also use launchd or cron with pmset sleepnow.

Why does my Mac wake up immediately after sleepnow?

This is often due to Power Nap, network access, or a USB device. Check pmset -g for enabled features like powernap or tcpkeepalive, and disable them if needed.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →