Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question
    
This might help: apple.stackexchange.com/questions/34884/… –  Jorge Jan 16 '12 at 22:18
    
You can also read this: stackoverflow.com/questions/1239439/… –  jm666 Jan 18 '12 at 17:03

10 Answers 10

up vote 19 down vote accepted
+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!

share|improve this answer
1  
+1 I was just about to post the same! I guess this is the best way. I'll up vote this as soon as I'm allowed again. I've reached my limit for today. :) –  gentmatt Jan 12 '12 at 14:57
    
Haha, that's awesome. Thanks. –  Matt Love Jan 12 '12 at 15:03
5  
You'd rather put it in /usr/local/bin, so as not to risk anything messed up in the system, have it survive potential system upgrades and whatnot. That's what /usr/local is for after all. –  Lloeki Jan 13 '12 at 9:57
2  
No we're not getting confused. This is dictated by the FHS. Every single opensource package worth its salt has a --prefix defaulting to /usr/local for this reason. On Mac OS X (which respects the FHS, mostly, except for /users which got inherited from some other unixes) /usr/local/bin, although nonexistent on the filesystem by default, is in the default PATH. Just create it. –  Lloeki Jan 14 '12 at 17:06
1  
Matt should get credit for the correct answer here, but just to note that you can run 'open -a SleepDisplay' via ssh and it will immediately turn off the screen (or, yes, you can do cp /Applications/SleepDisplay.app/Contents/MacOS/sleepdisplay /usr/local/bin/ if you want it in your path. Then you can just type sleepdisplay and it will, even over ssh, dim the monitor. I just tested this on my iMac, ssh'd from my MacBook Air. Both running 10.7. –  TJ Luoma Jan 17 '12 at 15:18

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.

share|improve this answer
    
It's not in 10.8; I just checked. –  adiabatic Nov 16 '13 at 0:30
1  
For some reason I don’t need escalated privileges on my machine (with a clean Mavericks install). –  mcb Mar 8 at 3:51
    
FYI: To use this with any keyboard shortcut you want to put the display to sleep you can use Automator -> New -> Service. Then search "run shell script" in the "actions" tab. Drag it to the right side. Instead of "cat" put "pmset displaysleepnow" as proposed above. Select "no input" from the "service receives selected". Save (cmd+s) it as whatever name ie. "SleepDisplay". Then go to System Preferences -> Keyboard -> Shortcuts -> Services and on the bottom of the list you find your command. Assign any shortcut you like. –  4485670 Aug 15 at 19:05

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/grep displaysleep | /usr/bin/awk '{print $2}'`
magic_number=2147483648
/usr/bin/pmset -a displaysleep $magic_number; sleep 1; /usr/bin/pmset -a displaysleep $original_setting
share|improve this answer
    
That didn't seem work if the original setting was 0 (never). You could add something like [[ $original_setting == 0 ]] && original_setting=180. –  ؘؘؘؘ Jan 17 '12 at 1:59
    
A minor note: using 'grep' to match a line to feed to 'awk' is extraneous; this is what 'awk' is for. Get rid of the grep and use awk /displaysleep/ {print $2}' –  kundor Feb 13 at 17:53

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.

share|improve this answer
    
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

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.

share|improve this answer
1  
Running shell scripts will delay system sleep, though the details aren't all there in my brain at the moment. –  Jason Salaz Jan 6 '12 at 0:50
1  
Yes something like that but just to turn the screen black (as with ctrl+shift+eject), not put the computer to sleep. It does stop the music for example. –  gregseth Jan 6 '12 at 6:33
1  
This is the answer. It works instantly and incudes a true sleep state. –  Miles Leacy Jan 13 '12 at 19:32
2  
@MilesLeacy Thanks, but that's not what the user asked for. Apparently it sleeps the machines completely (it didn't when I tested it). Keeping the answer up for others' reference only. –  Daniel Beck Jan 13 '12 at 19:56
    
On 10.9 (at least) this does sleep the entire system. –  Ryan Jun 28 at 22:11

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.

share|improve this answer
    
While this is really cool, there are two limitations: 1) Alfred has no option for displaysleep 2) These special commands in Alfred only exist when your system language is English. –  gentmatt Jan 16 '12 at 16:05
1  
@gentmatt While #1 is correct, #2 is wrong. My system language is Finnish and the special commands happily exist & execute. If you want to localize the triggering commands you can do it under Alfred's PreferencesFeaturesSystem CommandsSystem. –  koiyu Jan 16 '12 at 17:54
    
Great news! Thanks for that info. –  gentmatt Jan 16 '12 at 18:00

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

pmset -a displaysleep 1

(requires root)

share|improve this answer
2  
This is a good option as well, the only downside is that it will cause a one minute (hence the 1 at the end of the command) for the display sleep. Just thought Daniel Beck should know. –  Matt Love Jan 12 '12 at 16:26
4  
Isn't this permanent? If it's just supposed to be used once when needed you need to use force: sudo pmset force -a display sleep 1 –  gentmatt Jan 12 '12 at 17:09

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.

Here's the code:
# 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.

Hope this is useful to some!

-Bruce

ps: Sorry for the ugly code formatting. I'm not actually a programmer and this is my first time here.

share|improve this answer

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.

share|improve this answer
1  
This does not address his need to do this remotely or via ssh. –  TJ Luoma Jan 17 '12 at 14:36

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

share|improve this answer
1  
Doesn't this sleep the whole computer? –  patrix Jun 8 at 19:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.