简体   繁体   English

一个 bash shell 程序,用于等待列表中的所有服务器启动,然后运行启动脚本

[英]A bash shell program to wait for all servers in a list to start, then run startup scripts

I'm trying to automate the startup (after reboot) of an application that runs on a grid of 12 Linux 7 servers.我正在尝试自动启动(重新启动后)在 12 个 Linux 7 服务器的网格上运行的应用程序。

The 12 servers get rebooted in random order. 12 台服务器以随机顺序重新启动。

All the servers need to be running before I can start up the application that resides on the 12 servers.在我启动驻留在 12 个服务器上的应用程序之前,所有服务器都需要运行。

What I'd like to do is to test that all 12 Linux 7 servers are up and then when all 12 are up, I'd proceed with the startup sequence of commands.我想要做的是测试所有 12 个 Linux 7 服务器都已启动,然后当所有 12 个服务器都启动时,我将继续执行启动命令序列。

All 12 server are set up with ssh keys.所有 12 个服务器都设置了 ssh 密钥。

cat serverlist.txt 10.0.0.6 10.0.0.7 10.0.0.8 10.0.0.9 ... 10.0.0.18猫 serverlist.txt 10.0.0.6 10.0.0.7 10.0.0.8 10.0.0.9 ... 10.0.0.18

I want to ping a server and then wait until the ping is successful, then move to the next IP address.我想ping一个服务器,然后等到ping成功,然后移动到下一个IP地址。

My apologies for my question.我为我的问题道歉。

How to code this?如何编码?

Read in the first line from IP list file.从 IP 列表文件中读取第一行。 Ping first IP until success, then ping the other IP addresses (one at a time) until the success of all 12. ping第一个IP直到成功,然后ping其他IP地址(一次一个),直到所有12个都成功。

Then, run commands to start the application on the grid of 12 servers.然后,运行命令以在 12 个服务器的网格上启动应用程序。

Question: How to code this in the bash shell.问题:如何在 bash shell 中对此进行编码。

The inner loop can be as simple as内循环可以很简单

while ! ssh "${connection_string}" -o ConnectTimeout=5 true
do
    sleep 0.5
done

This runs a trivial command, and waits for 0.5 seconds between retries.这将运行一个简单的命令,并在两次重试之间等待 0.5 秒。

for i in `cat /home/Startup/serverlist.txt`
do
    ssh ${i} -o ConnectTimeout=5 true
    while test $? -gt 0
    do
        ssh ${i} -o ConnectTimeout=5 true
    done
done
exit  

If ssh works the server is alive, plus the use of the timeout is a better way to speed up the script execution.如果 ssh 工作,则服务器处于活动状态,加上使用超时是加速脚本执行的更好方法。 If the script get finish that means all the servers are up and responding.如果脚本完成,则意味着所有服务器都已启动并响应。

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

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