后台同步ssh
这是以下问题的后续: 在 bash shell 脚本中执行 ssh 命令 问题
我正在做类似的工作:
while read line
do
ssh -n root@$line 'command'
do something
done
是我只想在上一个 ssh 行中的“命令”执行完成时才执行“做某事”部分,即我想等待后台 ssh 完成。 我可以使用睡眠,但这不适合我的应用程序。请指导我。
我不想特别在后台运行 ssh 。我这样做只是为了让我的循环正常运行。如 在 bash shell 中执行 ssh 命令中给出的循环内的脚本 如果我们不给出 -n 标志,循环将在第一个 ssh 命令执行后终止。
This is a follow-up to the following question:
Executing ssh command in a bash shell script within a loop
I am doing a similar job:
while read line
do
ssh -n root@$line 'command'
do something
done
The problem is that I want to execute the "do something" part only when the execution of 'command' is complete in the previous ssh line ie I want to wait till the background ssh completes.
I can use sleep, but this does not suit my application. Please guide me.
I don't want to particularly run ssh in the background. I am only doing this so that my loop runs properly. As given in Executing ssh command in a bash shell script within a loop
if we dont give -n flag, the loop terminates after first ssh command execution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的命令不在后台运行,则执行 ssh 服务器命令将等待该命令完成。例如,试试这个:
这将在远程服务器上实际执行
sleep 3
后(即 3 秒后)打印“finished”。试试这个 - 在 ~/tmp 中创建三个文件 - 称为lines、parent.sh和child.sh。为了进行测试,请在运行 sshd 的计算机上创建它们,这样您就可以使用 ssh localhost 代替 ssh 服务器。将其放入其中:
行
<前><代码>第1行
第2行
3号线
parent.sh
子.sh
当您运行parent: 时,
输出将是这样的:
这表明child 将在parent 继续处理下一行之前执行。您将在“childprocessing”和“childdone”行之间看到 2 秒睡眠(在 child.sh 中执行
sleep 2
的结果)。请注意,
ssh -n
不适用于在后台运行ssh
- 它用于不消耗标准输入。要在后台运行 ssh,可以使用 ssh -f - 请参阅 man ssh。If your command is not run in the background, executing
ssh server command
will wait for that command to finish. For example, try this:This will print "finished" after
sleep 3
actually executed on the remote server (i.e. after 3 seconds).Try this - make three files in ~/tmp - called lines, parent.sh and child.sh. For a test, make them on some machine that has sshd running, so you can use
ssh localhost
instead ofssh server
. Put this in them:lines
parent.sh
child.sh
When you run parent:
the output will be this:
which shows that child will execute before the parent will continue processing a next line. You will see a 2 second sleep (a result of
sleep 2
executing in the child.sh) between "child processing" and "child done" lines.Note that
ssh -n
is not for runningssh
in the background - it's for not consuming stdin. To run ssh in background,ssh -f
can be used - seeman ssh
.一点谷歌搜索帮助我:
这样,就不需要 -n 标志。
欲了解更多详情:
http://72.14.189.113/howto/shell/while-ssh/
A little bit of googling help me:
In this way, there is no need of -n flag.
For more details:
http://72.14.189.113/howto/shell/while-ssh/