Skip to content
Advertisement

How to run remote script on multiple host simultaneously

I have a great amount of Linux servers to maintain. Frequently I need to run a script (script.sh) on all of them to get the health status, this script usually takes about 30-40 seconds to give an output. To facilitate maintenance tasks, I’m writing a shell script that uses SSH to loop through all remote hosts, run script.sh, collect output and write it to a log file in my local host. For the sake of this question, I have named this script MyScript.sh

The script works fine, however, it has to wait for the SSH output to continue to the next host. Because I have too many servers, and the commands runs in sequence, it take several minutes to finish. I would like to loop through all servers in parallel, without needing to wait for a response from each host.

Is there a way I can remotely run script.sh simultaneously on all host using MyScript.sh? Maybe run the ssh command in the background and somehow collect the output?

The output of script.sh is a single line separated by pipes. Such as the following

JavaScript

The output of Myscript.sh is the concatenation of the output from all host without pipes.

JavaScript

The core of MyScript.sh is the following:

JavaScript

In summary, I would like to optimize this script to run the above code simultaneously on multiple servers. Thanks!

Advertisement

Answer

The solution could be to deploy a monitoring software with custom checks.

For the parrallel ssh problem, without install any binaries you could use this script I wrote a while ago. Put in a file mssh, run chmod u+x mssh and then :

JavaScript

The mssh file :

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement