简体   繁体   English

如何通过shell脚本从machineX在machineA上执行命令?

[英]How to execute a command on a machineA from machineX through a shell script?

I have an application server running on Ubuntu 12.04 which I am starting and stopping by below commands - 我有一个在Ubuntu 12.04上运行的应用程序服务器,该服务器通过以下命令启动和停止-

For stopping the app server 用于停止应用服务器

david@machineA:/opt/kml$ /opt/kml/bin/kml_start_stop.sh stop

For starting the app server 用于启动应用服务器

david@machineA:/opt/kml$ /opt/kml/bin/kml_start_stop.sh start

Now I am trying to write a shell script which will stop and start my application server by using the above command but this shell script I will be running from machineX . 现在,我试图编写一个shell脚本,该脚本将使用上述命令停止和启动我的应用程序服务器,但是我将使用machineX运行该shell脚本。 And from machineX we can login passwordless without typing any password for user david to machineA machineX我们可以无密码登录,而无需输入用户davidmachineA任何密码

For example - From machineX I can do ssh david@machineA without typing my password. 例如-在machineX无需输入密码即可执行ssh david@machineA

Below is what I have started. 以下是我开始的内容。 Does everything looks right? 一切看起来都正确吗?

#!/bin/bash

ssh david@machineA

/opt/kml/bin/kml_start_stop.sh stop
sleep 10s
/opt/kml/bin/kml_start_stop.sh start

You could use a here document to execute remotely some script: 您可以使用here文档来远程执行一些脚本:

ssh david@machineA << EOJ
  /opt/kml/bin/kml_start_stop.sh stop
  sleep 10
  /opt/kml/bin/kml_start_stop.sh start
EOJ

in your case, you could also execute several ssh commands and sleep locally: 在您的情况下,您还可以执行几个ssh命令并在本地sleep

ssh david@machineA /opt/kml/bin/kml_start_stop.sh stop
sleep 10
ssh david@machineA /opt/kml/bin/kml_start_stop.sh start

BTW, I recommend learning more about shell scripting. 顺便说一句,我建议您了解有关Shell脚本的更多信息。 You could read Advanced Bash Scripting Guide (with a critical eye, it does have imperfections) and the documentation of GNU bash . 您可以阅读《 高级Bash脚本指南》 (确实有缺陷)和GNU bash文档 In particular, to do that on several machines, you might use some bash for loop. 特别是,要在多台计算机上执行此操作,可以使用一些bash for循环。

No, you cannot do that because ssh will login and wait for commands, whereas you want to feed the commands to the very same ssh. 不,您不能这样做,因为ssh将登录并等待命令,而您想将命令提供给相同的ssh。 The script should look more like this: 该脚本应该看起来像这样:

#!/bin/bash
while read host rest; do
  ssh david@$host <<EOF &
  /opt/kml/bin/kml_start_stop.sh stop
  sleep 10s
  /opt/kml/bin/kml_start_stop.sh start
  EOF
done < hostlist.txt
wait

That uses a Bash feature called a "here document" which feeds the text to the stdin of the ssh process. 它使用了一个称为“ here document”的Bash功能,该功能将文本输入ssh进程的stdin。

Edit: I added a small trick: I background each of the ssh jobs as they are started, because each one will take some time to complete (at least 10 seconds due to the sleep). 编辑:我添加了一个小技巧:在启动每个ssh作业时,我都会对其进行后台处理,因为每个ssh作业都需要一些时间才能完成(由于睡眠至少需要10秒)。 Then at the end I do wait to wait for all of them to complete. 然后最后,我确实wait所有这些完成。 This way, the entire script can run in 10 seconds plus the overhead time, regardless of how many servers there are. 这样,无论有多少服务器,整个脚本可以在10秒加上开销时间中运行。

This script would execute the commands on the remote terminal. 该脚本将在远程终端上执行命令。

#!/bin/bash

ssh david@machineA '/opt/kml/bin/kml_start_stop.sh stop && sleep 10s && /opt/kml/bin/kml_start_stop.sh start'

In case if you have used the below script then, a separate ssh session shell would be spawned to connect to the remote machine and the below commands would be run on the local machine shell instead on the remote 如果您使用了以下脚本,则会生成一个单独的ssh会话外壳程序以连接到远程计算机,并且以下命令将在本地计算机外壳程序上而不是在远程计算机上运行

#!/bin/bash

ssh david@machineA 
/opt/kml/bin/kml_start_stop.sh stop
sleep 10s
/opt/kml/bin/kml_start_stop.sh start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM