'Send shutdown signal to number of servers at ones
When I use following script to send shutdown command to numbers servers at ones it takes long time to shutdown the servers, even if i give 5 servers in the input file (manually shutdown is less time consuming). can this script be improved to work faster to send shutdown command to number of servers ?
#!/bin/bash
input="/home/ptx/input.file"
while IFS= read -r line;
do
</dev/null sshpass -f pass_file ssh -tt user1@line 'echo password | /usr/bin/sudo -S-s
shutdown -h now'
done < "$input"
Solution 1:[1]
Yes, it can. Firstly, you need to put a $ in front of line so it actually connects to the host. Secondly, in this form it will shut hosts down sequentially, not in parallel. Put the jobs in the background and wait for them all to complete before proceeding.
Also - consider using ssh keys with an agent, not sshpass. Then you can run:
while read HOST; do ssh -qt "$HOST" shutdown -h now" & done < "$input"; wait
Note the ampersand instead of the semi colon to send the job to the background, and the wait statement to wait till they're all complete.
Read the JOB CONTROL section of man bash for more information on backgrounding tasks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | garethhumphriesgkc |
