Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I guess this question is pretty much the same as How to configure dbus to allow ssh-user to suspend server?; except I wanted to formulate it better.

Assume I have an Ubuntu server running nonstop, let's call it MyServer. Let's say then, I have another PC on the same local network as MyServer, let's call it MyLocalPC.

MyLocalPC is then kept in suspend, unless it is turned on via wakeonlan by MyServer. This part of the process works perfectly for me: I can log in via ssh to MyServer, and on the MyServer ssh prompt I can issue wakeonlan, and MyLocalPC wakes up - great.

Then, however, as soon as I'm done working with MyLocalPC, I'd like to put it into suspend again. The problem is as follows - I can put the machine in suspend easy, if I ssh into MyLocalPC first via ssh:

[MyServer]$ ssh MyLocalPC
[MyLocalPC]$ dbus-send --session --dest=org.freedesktop.PowerManagement --type=method_call --print-reply --reply-timeout=2000 /org/freedesktop/PowerManagement org.freedesktop.PowerManagement.Suspend

This will suspend MyLocalPC - but will also block the ssh exiting properly, and eventually the ssh to MyServer ends up frozen. I try to get tricky and issue something like this:

[MyServer]$ ssh MyLocalPC "dbus-send --session --dest=org.freedesktop.PowerManagement --type=method_call --print-reply --reply-timeout=2000 /org/freedesktop/PowerManagement org.freedesktop.PowerManagement.Suspend"
Failed to open connection to "session" message bus: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

Then I read somewhere DISPLAY variable should be defined - but that fails too:

[MyServer]$ ssh MyLocalPC "DISPLAY=:0 dbus-send --session --dest=org.freedesktop.PowerManagement --type=method_call --print-reply --reply-timeout=2000 /org/freedesktop/PowerManagement  org.freedesktop.PowerManagement.Suspend"
Error org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.PowerManagement was not provided by any .service files

So does anyone know how can I issue a suspend to MyLocalPC, without the ssh connection to MyServer freezing?

share|improve this question
add comment

5 Answers

Just type <enter>~& (that's three keystrokes: enter, squggle, ampersand) and ssh will go into the background, and eventually timeout by itself. That's good if you want to just abandon an interactive session in a case like this.

Or, alternatively, put that into a shell script on MyLocalPC and connect in a way that will background the connection:

ssh -f MyLocalPC go-to-sleep
share|improve this answer
    
Thanks @poolie for the answer - will have to report back with what I ended up choosing, I just wanted to +1 for now :) Cheers! –  sdaau Apr 18 '11 at 10:52
add comment

You need to enable the X11 forwarding using ssh -Y. Alternatively, use at now + 2 minutes to run the command only once (see man at for more information).

share|improve this answer
    
Thanks for the answer, @Adam Byrtek - good info on X11 forwarding.. –  sdaau Apr 18 '11 at 10:53
add comment

set a cron job for 2 minutes in the future and disconnect

share|improve this answer
    
Hi @aking1012 - thanks for that, I'll give it a try and report back.... Cheers! –  sdaau Apr 18 '11 at 7:02
2  
at would be a better choice than cron if you just need a single job to run a bit later, or even better just sleep 2m && blah. –  poolie Apr 18 '11 at 9:08
    
@poolie - good point –  hbdgaf Apr 18 '11 at 9:25
add comment

For me the simplest is to use the at command:

echo 'pm-suspend' | sudo at now + 2 minutes

Then disconnect from SSH session.

share|improve this answer
add comment

How can I break out of ssh when it locks? There are many valid solutions. One has been mentioned by poolie; I have set up a keep-alive timeout on my system that will auto-suspend hanging SSH sessions by adding the following to ~/.ssh/config:

Host *
ServerAliveInterval 5
ServerAliveCountMax 4

Works also after suspending the remote machine.

share|improve this answer
add comment

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.