Bash 在上一个 ssh 命令完成之前运行命令

Bash 在上一个 ssh 命令完成之前运行命令

我正在尝试为为两台服务器供电的 UPS 编写一个关闭协议。我希望服务器 A 通过 SSH 在服务器 B 上触发 bash 脚本。我正在测试的脚本在触发服务器 B 方面工作得很好,但在继续执行脚本的其余部分之前,我在等待来自 SSH 的回复时遇到了问题。到目前为止,这是我的服务器 A 脚本:

#!/bin/bash

ssh [email protected] "/bin/bash /opt/shutdown.sh"

sleep 10
echo "start shutdown" | wall
shutdown -p now

我想我需要第一条线来独立运行吗?我做了一些搜索,并尝试过在 ssh 行的末尾nohup添加。&我也尝试过使用screen但没有任何运气。我希望脚本在调用 ssh 命令后立即继续。任何帮助将非常感激。

答案1

根据 ssh 人的说法:

 -f      Requests ssh to go to background just before command execution.  This is useful if ssh is going to ask
         for passwords or passphrases, but the user wants it in the background.  This implies -n.  The recom‐
         mended way to start X11 programs at a remote site is with something like ssh -f host xterm.

我想可能会解决你的问题欢呼!ssh -f [email protected] "/bin/bash /opt/shutdown.sh"

相关内容