Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BlueRevolution

macrumors 603
Original poster
Jul 26, 2004
6,054
2
Montreal, QC
I wrote the following shell script to sleep my computer from the command line:

/usr/bin/sleepy:
#!/usr/bin/osascript
tell application "system events" to sleep


It works perfectly when run from Terminal or over a ssh connection while the computer is running normally, but while the display is asleep and the command is run over ssh, it does nothing, even though all other commands work fine. Since the point was to be able to sleep the computer remotely, that's a bit of a serious flaw.

Any suggestions?
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
39,777
7,497
Los Angeles
Just to get the obvious question out of the way, are you sure that it's the case where they display is sleeping, as opposed to when the computer is sleeping? Do you actually get a connection in which to use the command?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
I'm surprised at this. I would have the thought the network activity produced by the remote connection would have prevented the computer from sleeping.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
This worked for me, with display sleeping or not:
Code:
[B]cat sleepy[/B]
#!/bin/bash
/usr/bin/osascript -e 'tell app "System Events" to sleep'

So did this command:
Code:
sudo pmset sleepnow
 

BlueRevolution

macrumors 603
Original poster
Jul 26, 2004
6,054
2
Montreal, QC
Just to get the obvious question out of the way, are you sure that it's the case where they display is sleeping, as opposed to when the computer is sleeping? Do you actually get a connection in which to use the command?

Yes. As I said, I can ssh in and run other commands while the display is asleep.

I'm surprised at this. I would have the thought the network activity produced by the remote connection would have prevented the computer from sleeping.

Nope, when the computer sleeps it just stops responding to subsequent commands. Can't wake it up again remotely via ssh, only (by my understanding) with a wake-on-wireless-LAN–enabled computer and an Apple router.

This worked for me, with display sleeping or not:
Code:
[B]cat sleepy[/B]
#!/bin/bash
/usr/bin/osascript -e 'tell app "System Events" to sleep'

No good. That's the same command.

So did this command:
Code:
sudo pmset sleepnow

Perfect! I'd rather not have to sudo up to run it, but it seems to work fine without sudo. Just rewrote that as a shell script and presto.

Thanks for the help, folks.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,706
8,346
A sea of green
If pmset doesn't need sudo, then you're probably logged in as root. That might affect the running of AppleScripts.

I wasn't logged in as root when I ran my tests.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.