我正在尝试将ssh放入多个aws节点,并在停止时启动一个服务(它包含一个服务列表)。以下是我尝试过的脚本,我对Shell脚本是完全陌生的,如果这是实现我要尝试的更好方法,请提出建议。
#!/bin/bash
service_1="service_1"
service_2="service_2"
service_3="service_3"
service_4="service_4"
service_5="service_5"
service_6="service_6"
check_service()
{
service_status=$(pgrep "$1" | wc -l)
if [ "$service_status" -ne 1 ]
then
echo "starting service $1"
"$1 start"
else
echo "stopping service $1"
"$1 stop"
fi
}
# Reading the node.list line by line
while read -r line
do
{
echo "running ssh on $line"
# specify the directory of the pem file location
ssh -i /directory/keyname.pem ec2-user@"$line"
# Passing the different service variables to check_service method
check_service "$service_1"
check_service "$service_2"
check_service "$service_3"
check_service "$service_4"
check_service "$service_5"
check_service "$service_6"
} < /dev/null
done < "node.list"