How do I suspend over ssh?

How do I suspend over ssh?

我想这个问题和如何配置 dbus 以允许 ssh 用户暂停服务器?;除非我想更好地表述它。

假设我有一台不间断运行的 Ubuntu 服务器,我们称之为MyServer。假设我在同一个本地网络上有另一台 PC MyServer,我们称之为MyLocalPC

MyLocalPC然后保持暂停状态,除非通过 打开它wakeonlanMyServer这个过程的这一部分对我来说非常完美:我可以通过 登录sshMyServer然后在MyServerssh 提示符下发出wakeonlan,然后MyLocalPC唤醒 - 太棒了。

但是,一旦我完成了 的工作MyLocalPC,我想再次将其置于挂起状态。问题如下 - 如果我MyLocalPC先通过ssh 进入,我可以轻松地将机器置于挂起状态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

这将暂停MyLocalPC- 但也会ssh正确阻止退出,最终导致sshtoMyServer被冻结。我尝试使用一些技巧并发出类似这样的命令:

[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.

然后我读到某处DISPLAY应该定义变量 - 但是也失败了:

[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

那么,有人知道我怎样才能发出暂停命令MyLocalPC,而不ssh连接MyServer冻结吗?

答案1

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

答案2

For me the simplest is to use the at command:

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

Then disconnect from SSH session.

答案3

I know this is a very old question, but on Ubuntu 22.04, I've been using

sudo systemctl suspend && exit

to suspend the machine and immediately exit the SSH connection.

答案4

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

相关内容