我有一个如下所示的脚本:
SERVER="RHT372 RHTl373 RHT374 RHTl375"
case $1 in
"stop")
for SERVER in $SERVER
do
echo "================================================"
echo Stopping $SERVER
ssh root@$SERVER /var/tmp/stopServer.sh server1
done
;;
我怎样才能让 ssh 等待命令完成然后转到下一个服务器并发出相同的命令?
答案1
举个例子,请看这个:
#!/bin/bash
(( $# != 1 )) && printf "%s\n" "Please provide one parameter"; exit 1;
servers=( srv1 srv2 srv3 )
case $1 in
"stop")
for srv in "${servers[@]}"
do
hostAlive=$(ping -s 64 "$srv" -c 1 | grep packet | awk '{print $(NF-4)}')
echo "=========="
echo "Stopping $srv"
[[ "$hostAlive" == "0%" ]] && ssh root@$srv '/var/tmp/stopServer.sh server1'
done;;
"start")
for srv in "${servers[@]}"
do
echo "=========="
echo "Starting $srv"
ssh....
done;;
*)
echo "Not a valid option"
exit 1;;
esac
ssh 命令仅在前一个命令完成操作后才会运行。如需了解更多信息,请参阅wait
命令。
答案2
一台服务器关闭需要多长时间?
您可以sleep
在服务器关闭期间使用。
用法:
echo "Hi, I'm sleeping for 5 seconds..."
sleep 5
echo "all Done."