一键关服务脚本
stop.sh文件
#!/bin/bash
CNF_FILE="stop.cnf"
CNF_LINES=(`cat $CNF_FILE`)
count=0
for item in ${CNF_LINES[@]}
do
((count++))
item=(${item//->/ })
echo "$count、${item[0]}"
done
read -p '请选择:' choice
#验证用户输入必须为小于等于配置文件的行数的整数
if [[ $choice =~ ^[0-9]+$ ]]
then
if [ $choice -gt $count -o $choice -lt 1 ]
then
echo "选择必须是在1和$count之间,脚本将会退出"
exit 0
fi
else
echo '选择必须是一个数值,脚本将退出'
exit 0
fi
#自定义函数调用服务自带脚本关闭服务
function stopSers(){
LINE=$1
LINE=(${LINE//@/ })
SERS=${LINE[0]}
SERS=${SERS//,/ }
count=0
for item in $SERS
do
RST=`jps -ml|grep -w $item`
if [[ $RST ]]
then
count=1
break
fi
done
if [ $count -eq 1 ]
then
RST=`${LINE[1]}`
echo "STOPPED"
else
echo "NONE"
fi
}
#z自定义函数通过系统通用函数kill关闭服务
function killSers(){
LINE=$1
LINE=${LINE//,/ }
count=0
for item in $LINE
do
PID=`jps -ml|grep -w $item|awk {'print $1'}`
if [[ $PID ]]
then
PID=`kill -9 $PID`
((count++))
fi
done
if [ $count -gt 0 ]
then
echo "STOPPED"
else
echo "NONE"
fi
}
#自定义函数根据参数类型和操作字符串关闭服务
function stopByLine(){
SIGN=$1
TYPE=$2
LINE=$3
case $TYPE in
'STOP')
LINE=${LINE//;/ }
for item in $LINE
do
RST=`stopSers $item`
item=(${item//@/ })
item=${item[0]}
case $RST in
"STOPPED")
echo 'services_[_'$item'_]_stopped '
;;
"NONE")
echo 'no_services_[_'$item'_]_existed '
;;
esac
done
;;
'KILL')
RST=`killSers $LINE`
case $RST in
"STOPPED")
echo 'services_[_'$LINE'_]_stopped '
;;
"NONE")
echo 'no_services_[_'$LINE'_]_existed '
;;
esac
;;
esac
}
#根据用户的选择向后关闭服务
count=0
while(( $count<$choice ))
do
LINE=${CNF_LINES[$count]}
LINE=(${LINE//->/ })
SIGN=${LINE[0]}
TYPE=${LINE[1]}
LINE=${LINE[2]}
RST=`stopByLine $SIGN $TYPE $LINE`
for item in $RST
do
echo ${item//_/ }
done
((count++))
done
stop.cnf文件
hive->KILL->HiveMetaStore,HiveServer2
hadoop->STOP->NameNode,SecondaryNameNode,DataNode@stop-dfs.sh;ResourceManager,NodeManager@stop-yarn.sh