46

I'd like to be able to turn my screen off from command line.

I know there's the keyboard shortcut ctrl++ but I want to do it from a shell script or remotely.

Is there a way?

15 Answers 15

66

In OS X 10.9, you can simply do pmset displaysleepnow. This will immediately turn off your display without changing any of your settings or putting the entire host to sleep.

Escalated privileges don't seem to be required (at least with recent versions of OS X), but if you get a message about inadequate privileges, you could do sudo pmset displaysleepnow.

Based on feedback from commenters, this is not available in OS X 10.8 or earlier.

22
+50

While I haven't been able to find a command that will sleep the display natively, there is an app you download that will do it. There are two options from here.

  1. Install the app to the Applications folder and from Terminal or SSH run open /Applications/Sleep\ Display.app

  2. Right click on the app from the Downloads folder and click "Show Package Contents". Navigate to Contents/MacOS and copy the sleepdisplay file.

    Navigate to /usr/bin (you can use the ++G shortcut and type the directory in) and paste the binary file. You will now be able to type the command sleepdisplay into Terminal or through SSH and the display will immediately go to sleep.

Hope this helps!

7

Here's a simple shell script that will do it.

#!/usr/bin/env bash
open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app

This will run/start whatever settings you have enabled for Screen Saver on that computer.

  • I like this one. I copy/pasted this code into a file /usr/local/bin/ss, chmod +x on it, and can now type 'ss' in a commandline to make the screen saver activate. The screen saver in turn is configured to ask for a password one minute after activating the screen saver (which could be lowered further, I believe, if needed) – fwielstra Jan 27 '12 at 10:15
7

The following script will do the job (in Leopard and later), but it must be run with sudo:

The premise is that pmset can set a time until display sleep, but the problem is that a value of 0 turns the feature off, rather than setting the delay to zero, and a value of 1 is a full one minute delay. The magic here is that a value of 2^31 seems to be stored as negative zero, which magically functions as "turn the display off immediately".

In Tiger and earlier, a different magic number was needed, because a different bit-sized variable was used internally to store the delay, in minutes, until the display turns off.

This mimics the behavior of control-shift-eject, and can be used on MacBook Airs without an eject key.

#!/bin/bash
original_setting=`/usr/bin/pmset -g | /usr/bin/awk '/displaysleep/ {print $2}'`
magic_number=2147483648
/usr/bin/pmset -a displaysleep $magic_number; sleep 1; /usr/bin/pmset -a displaysleep $original_setting
5

on macOS Sierra

  • sleep display : pmset displaysleepnow
  • wake display : caffeinate -u -t 1
  • test state : pmset -g powerstate IODisplayWrangler | tail -1 | cut -c29 results <4 are a sleeping display

a small node HTTP server to set screen status of your Mac remotely: https://github.com/ycardon/switch-api

  • You can get a 0 or non-zero exit code for conditional logic by doing "echo $[$(pmset -g powerstate IODisplayWrangler | tail -1 | cut -c29) - 4]" (using this to true/false "is screen on?"). – atwixtor Jun 20 at 5:30
4

Run the following command to execute a short AppleScript that puts the display to sleep:

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

The command name suggests that it might put the entire system to sleep under some circumstances, but I could not make that happen in limited testing: I tested it with a shell script I started shortly before executing this command, and that was running for a few minutes until I "awoke" the system. It had continued to print output during the entire time.

2

Really hope this answer isn't too off the beaten track. My favourite way to sleep, restart, shut down - and most importantly lock - a Mac is using Alfred (the app launcher).

It doesn't require any scripts, knowledge of scripts or use of terminal, which is brilliant.

However, if you specifically WANT to use terminal, this probably isn't the solution for you.

2

Wake: caffeinate -u -t 2

Sleep: pmset displaysleepnow

1

You could use the pmset command to change the value for displaysleep, something like

pmset -a displaysleep 1

(requires root)

1

Take a look at the open source github project maclock

By default, it just launches the screensaver, but you can put the display to sleep with:

maclock --display

Under the covers, it is a bash script that is essentially just calling:

pmset displaysleepnow
1

Well, this is an old one, but it seems there aren't really good answers for this question anywhere.

I've gotten an AppleScript to work, though it requires a third-party, unmaintained, and un-registrable app—Extra Suites. It can be downloaded on the developer's old website.

# Gets the current state of the upper left hot corner, then sets it to sleep display.
tell application "System Events"
  tell expose preferences
    set givenActivty to get the activity of the top left screen
    set the properties of the top left screen corner to {activity:sleep display}
  end tell
end tell

# Uses Mouse to Activate upper left hot corner. [Moving directly to {0, 0} does not work
tell application "Extra Suites" 
  ES move mouse {1, 1}
  ES move mouse {0, 0}
end tell

# Restores state of upper left hot corner.
tell application "System Events"
  tell expose preferences
    delay 1
    set the activity of the top left screen corner to givenActivty
  end tell
end tell

# Gets rid of Extra Suites nag window.
tell application "Extra Suites"
  quit
end tell

I've also incorporated it into an Alfred Workflow.

1

I did it with sudo shutdown -s now.

Note that you will need administrator privilege to do this though.

-1

Using Alfred (free at the AppStore) is a great way to put the display to sleep by just writing: "sleep". Doesn't get simpler than that.

Of course Alfred does much more than that, you can also restart, logout, empty trash, lock, shutdown and many more things.

  • 1
    This does not address his need to do this remotely or via ssh. – TJ Luoma Jan 17 '12 at 14:36
-2

No need extra app, just use tell application "Finder" to sleep

  • 1
    Doesn't this sleep the whole computer? – nohillside Jun 8 '14 at 19:39
-2

The app Launchbar works equally well as Alfred. In my case, CMD spacebar then S [Sleep] - simple and fast

  • This does not answer the question. gregseth is not looking for a way to put the computer to sleep with a third-party utility, but a way to put the display to sleep from the command line. – Timothy Mueller-Harder Nov 25 '16 at 21:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .