27

This is a two part question. A practical, "how to" question is first, and then I ask for configuration suggestions.

First, I put my displays to sleep at night, and I would like to be able to wake the display remotely. Is there a command line utility I can use to wake it via SSH? I know about WakeOnLan, but that seems like overkill.

Second, the whole reason for me wanting to wake my display remotely has to do with how Lion's power savings settings changed. The Apple infrared remote no longer wakes the display. This is a huge deal for a machine I originally bought to be a combined desktop/home theater pc, since it defeats half of its purpose. So, does anybody have any ideas/suggestions what to do about this? This is open ended, I know.

7 Answers 7

35

The easiest way to do this over SSH is to use caffeinate -u (available since OS X 10.9 I think) to simulate user activity.

Add the flag -t 5 to specify a timeout of 5 seconds (else you'd have to ctrl+c out of caffeinate, it seems).

4
  • 1
    there you go a (+) for your answer that no one has been paying attention to. I have so many of those my self, where ppl come ask a question newer to come back to check for a answer.
    – Ruskes
    Commented Feb 27, 2015 at 17:36
  • 1
    According to the man page (in High Sierra), the 5 sec is a defaul for the -u option: If a timeout is not specified with '-t' option, then this assertion is taken with a default of 5 second timeout
    – JJarava
    Commented Jun 26, 2019 at 7:37
  • @JJarava I just tried it without the '-t' option and, while it woke up the remote machine, it didn't seem to timeout at all (I gave up waiting after about a minute).
    – metadaddy
    Commented Sep 20, 2021 at 14:42
  • Didn't work on Monterrey (using my company's security profile - not sure if that affects it). Commented Mar 16, 2022 at 1:25
18

Wake: caffeinate -u -t 2

Sleep: pmset displaysleepnow

1
  • Nice solution! Never though of using caffeinatefor turning the display on, but I guess it works.
    – JMY1000
    Commented May 8, 2016 at 5:19
10

You can use

pmset schedule wake "01/01/2012 20:00:00"

to wake up a sleeping display on an otherwise "awake" Mac. Replace the date/time part with current time of course.

7

Waking the Display from the Command Line

For waking your display remotely, try running an AppleScript on the command line:

osascript -e 'tell application "System Events" to key code 123'

This will simulate pressing the cursor key and should make Mac OS X 10 believe a user is active on the computer.

This AppleScript is based on the answer, How do I automate a key press in AppleScript?

Waking the Display after Waking with the Remote

If you are waking your Mac using the remote and the display is not powering on, try Wake Assist. It is not designed for this situation but might just work.

Wake Assist simulates a mouse click after your Mac wakes up. The mouse is clicked twice in the upper-left of your main display. Wake Assist will do nothing if it detects a user but if the computer wakes and is idle, then the mouse is clicked for you.

3
  • 1
    The AppleScript thing did not work for me. I get a black image from screencapture even after waiting a few minutes.
    – lhf
    Commented Jul 18, 2012 at 18:02
  • Have you tried any other simulated key presses - maybe the cursor key is not enough? Do you require a password on wake? Commented Jul 19, 2012 at 9:34
  • 1
    Worked fine for me on OS X 10.6
    – claytron
    Commented Nov 14, 2012 at 23:15
3

wuc wrote:

You can use

pmset schedule wake "01/01/2012 20:00:00"

to wake up a sleeping display on an otherwise "awake" Mac. Replace the date/time part with current time of course.

However, that didn't work for me on a circa 2008 iMac running 10.9.1 or a late 2010 MacBook Air running 10.9.2. I'm not sure if this has something to do with Mavericks' energy management or the hardware, or what.

I was able to get it to work by setting the wake time to be 15 seconds into the future. Occasionally I was able to get it to work with the setting as low as 12 or 13, but not reliably. But there may have been other factors I didn't realize at the time, but 15 worked, so I used 15.

But how do you calculate 15 seconds into the future programmatically?

I used gdate from the GNU Coreutils package (date in OS X might be able to do this, but if it can, I don't know how, and I already had gdate installed):

[to use date instead of gdate use alias set_wake_time='date "-v+${OFFSET}S" "+%D %T"']

Here's the script I used:

#!/bin/zsh -f

# how many seconds into the future we want to wake the display
# 15 seems to work reliably. YMMV.
OFFSET=15

# to calculate the time, we need `gdate`
alias set_wake_time='/usr/local/bin/gdate --date "+${OFFSET} sec" "+%m/%d/%g %H:%M:%S"'

# this is where we set the wake command
# if it doesn't succeed the script will exit immediately
/usr/bin/sudo /usr/bin/pmset schedule wake "`set_wake_time`" || exit 1

# if you were not testing this, you'd probably want to end at the
# next line. Just remove the leading '#'
#exit 0
#######################################################

### Everything below this line is only needed during testing ###

# this tells the display to sleep
# because we can test waking the screen up unless it's asleep
pmset displaysleepnow

# for testing purposes: now the script will pause for $OFFSET seconds
sleep $OFFSET

# For testing purposes:
# after $OFFSET seconds, this sound will play 3 times.
# by that time, the display should be awake
# I did this to help me know when I had set OFFSET too low

afplay /System/Library/Sounds/Glass.aiff

afplay /System/Library/Sounds/Glass.aiff

afplay /System/Library/Sounds/Glass.aiff

# script is done
exit 0

Everything after the '#######################################################' can be removed once you have finished testing.

3
  • date -jv+15S will do the trick of calculating the date 15 seconds in the future from now. The -j switch prevents date from trying to set the system clock.
    – mike
    Commented Apr 4, 2014 at 15:29
  • 2
    The command date -jv+15S "+%m/%d/%Y %H:%M:%S" will put the 15 seconds in the future into the correct format for pmset.
    – Aaron D
    Commented May 1, 2015 at 7:47
  • According to the OSX 10.10.3 pmset manpage, there is a 'pmset relative wake seconds' option as well. Might be worth investigating as well.
    – JS.
    Commented Jul 15, 2015 at 21:05
1

Oh, how I wish I had enough rep to upvote @wuc's answer the 10 times it deserves for this maddeningly frustrating problem (or to at least be able to leave a simple comment with the praise it deserves).

Apple, in its paternalistic wisdom, added a "feature" that intentionally made it harder to wake the display on Lion and Mountain Lion. This has made life much harder for those desiring to remotely wake the display (using IR remotes or via VNC) to interact with their Macs . For some reason, this became more pronounced with OS X 10.8.2.

Using pmset was a marvelous workaround for me.

Step 1: Login to the remote machine using ssh [email protected] (replace with an appropriate username and ip for the remote Mac).

Step 2: Wake up the display using pmset schedule wake "06/27/2013 04:20:00" (with an appropriate future date of your choosing).

1
0

Option if you don't want to use gdate:

  • Enable SSHD (Remote Login) under System Preferences, Sharing
  • Enable 'Wake for network access' under System Preferences, Energy Saver

put this code in a file like 'wakeup.sh' in your home folder:

shopt -s expand_aliases
#NB: 7 is the waiting interval. Works for me. Increase as needed. 
alias myDate='date -v+7S "+%m/%d/%Y %H:%M:%S"'
echo MacBook will wake up at: `myDate`
echo yourMacPW | sudo -S pmset schedule wake "`myDate`"
  • Make it executable with 'chmod +x wakeup.sh' and enter your password if you like. Otherwise drop the echo and -S.

If you want to automate this process to work from a windows machine:

  • Get PuTTY and a command line WakeOnLan tool (ie Aquila on SourceForge)
  • Make a one line text file containing './wakeup.sh'
  • write a batchfile like this:

    @echo off
    c:
    cd "C:\Program Files (x86)\Aquila Technology\WakeOnLAN"
    :tryAgain
    wakeonlanc.exe -w -mac 00:00:00:00:00:00
    ping 192.168.1.101 -n 1 | find "unreachable"
    if %ERRORLEVEL% EQU 0 GOTO tryAgain
    cd C:\putty
    putty.exe -ssh 192.168.1.101 -l yourUser -pw yourMacPW -m c:\putty\macWake.txt
    

Things you need to change: your IP, your MAC address, the word 'unreachable' may not be what you get - you might get 'timed out'. Storing a root password in plain text is of course, not a good idea, but this how to do it completely automatically from Windows to OSX.

You must log in to answer this question.

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