我有一个bash脚本,它在一个循环中ssh到不同的机器,如果传递了reboot = y参数(使用jenkins),这个脚本也会重新启动机器。
问题 - 如果reboot = y,则无法逐个循环每个主机的ssh命令,然后echo"重启主机"然后最后运行sh app-services.sh $ APP
我的目标是 - 重启完成后,我希望它一个接一个地ping主机,如果ping返回成功,则ssh回到机器并重新启动托管应用程序的服务。
我目前的剧本 -
#!/bin/bash
IP_S="${HOSTS_STAGE}"
IP_P="${HOSTS_PROD}"
APP="$APPLICATION"
export APP
export host
if [ "${ENVIRONMENT}" == 'stage' ]; then
IP="${IP_S}"
elif [ "${ENVIRONMENT}" == 'prod' ]; then
IP="${IP_P}"
fi
echo "${IP}"
for host in ${IP}; do
echo "########################################"
echo "## Running yum update on ${host}"
echo "########################################"
if [ "${REBOOT}" == 'n' ]; then
ssh -i "${id_rsa}" -o StrictHostKeyChecking=no dvopsadmin@${host} "sudo yum update -y"
echo "## Skipping reboot for ${host}"
else
ssh -i "${id_rsa}" -o StrictHostKeyChecking=no dvopsadmin@${host[@]} "sudo reboot"
echo "## Rebooting ${host}"
ping -w 30 -c 1 ${host}
# This is the other script that restart app services
sh app-services.sh $APP
fi
done