Jump to content

Sleep another Mac Remotely


Recommended Posts

Good morning, this is one I've been trying to accomplish a few times but have hit walls many times as my knowledge is limited.

 

I have a computer in the office (imac) that we use as a display only. I want a simple workflow to run on my macbook that would sleep the machine. I can do it with a few commands manually by SSH into the machine, then run osascript -e 'tell application "System Events" to sleep'.

 

But for some reason I cant get past this. I see 2 ways forward and havent figure out how to do it either way.

 

Firstly I believe theres a way to send the command via SSH without first logging in, but I cant get this to do anything so im sure my syntax is wrong, I am trying to do it like this

 

ssh username@192.168.1.xx 'osascript -e 'tell application "System Events" to sleep''

 

so im sure thats wrong as when I run that from my computer nothing at all happens. 

 

So in light of that not running I was thinking, make alfred run the ssh command, then paste in the second part osascript -e 'tell application "System Events" to sleep' press enter, wait then clsoe terminal, but for some reason this isnt working either, which makes no sense as Im doing the same thing I manually do just through alfred simulations

 

Not really sure what to do, anyone got any ideas? I feel like its really simple and I just lack the knowledge

 

Link to comment
28 minutes ago, ixium said:

Firstly I believe theres a way to send the command via SSH without first logging in

 

A machine that didn't require you to log in would be incredibly insecure. What you're looking for is key-based authentication, which logs you in without needing your password. In fact, it's a good idea to disallow password-based authentication when you have your keys working.

 

28 minutes ago, ixium said:

im sure my syntax is wrong

 

Your quoting is wrong. And it's very difficult to pass a command with quotes in quotes because you need to escape the escapes.

 

It's way simpler to save your command as a script and then run that via ssh:  ssh the.host.name osascript Desktop/sleep.scpt

Edited by deanishe
Link to comment

@ixium, since you're logging into a remote shell anyway, you could instead use a shell command to send the computer to sleep:

ssh <host> pmset sleepnow

 

@deanishe, how do you do inline code formatting ?  Or could you direct me to a page for formatting posts on this forum ?  Many thanks.

Link to comment

Hmm so the code is working great (was having a hard time with being on the right network) but now Im running into how to close terminal after the command is complete. I can send through the command+q command and that works well, except when the command is still running, any way to make it exit after finishing the command? I know im knit-picking now so no problem if not! Thank you both for your amazing help

Link to comment
37 minutes ago, ixium said:

but now Im running into how to close terminal after the command is complete.

 

It sounds like you're using Alfred's Terminal Command, which is not appropriate. That's only for when then command is interactive or you need to see the output.

 

Use a Run Script with Language = /bin/bash instead.

 

FWIW, when a Terminal Command is appropriate, and you want to close the session when the command exits, add && exit to your command.


That will tell the shell to exit if the previous command is successful. It you want the shell to exit even if the command failed, add ; exit:


ssh host.domain.com pmset sleepnow && exit

Edited by deanishe
Link to comment
6 minutes ago, ixium said:

Wouldnt I need to call terminal and everything

 

No. I just told you how to run it. Add a Run Script action with Language set to "/bin/bash" (which is exactly what Terminal runs when you start a new session).

 

Then paste your command in the box.

 

A terminal is a GUI program (usually) for running a shell. The shell (e.g. bash or zsh or fish) is the program you're actually using. That's the software that you're passing the ssh command to. All Terminal does is pass your input to the shell and make its output look pretty.

Edited by deanishe
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...