集群一键启动/停止脚本
集群一键启动/停止脚本
1.集群一键启停脚本:mysh
#!/bin/bash
#1 获取输入参数个数,如果没有参数,直接退出
p1=$1
p2=$2
user=`whoami`
FT=`dirname $0`
FT=`cd $FT; pwd`
ARGS_CONFIG=$FT/args_config.pro
# 获取参数
LINUX_VERSION=`grep -w LINUX_VERSION $ARGS_CONFIG |gawk -F"=" '{ print $2 }'`
HOSTS=(`grep -w HOSTS $ARGS_CONFIG |gawk -F"=" '{ print $2 }'`)
ZOOKEEPER_HOME=`grep -w ZOOKEEPER_HOME $ARGS_CONFIG |gawk -F"=" '{ print $2 }'`
HADOOP_HOME=`grep -w HADOOP_HOME $ARGS_CONFIG |gawk -F"=" '{ print $2 }'`
HIVE_HOME=`grep -w HIVE_HOME $ARGS_CONFIG |gawk -F"=" '{ print $2 }'`
# 打印使用说明
function print_usage(){
echo "Usage: mysh jps|zk|hadoop|hive [start | stop]"
echo "mysh options: describe:"
echo " jps 查看三台设备启动的java程序"
echo " zk|zookeeper start|stop 启动或停止zookeeper"
echo " hadoop|hdp start|stop 启动或停止hadoop"
echo " hive|hiveserver2|hs2 start|stop 启动或停止hive(hiveserver2)"
echo "Examples:"
echo " mysh jps"
echo " mysh zk start"
}
# 检测Hadoop是否启动
function check_hadoop(){
nncount=`mysh jps |grep NameNode |wc -l`
dncount=`mysh jps |grep DataNode |wc -l`
nmcount=`mysh jps |grep NodeManager |wc -l`
rmcount=`mysh jps |grep ResourceManager |wc -l`
if [ $nncount -eq 2 -a $dncount -eq 3 -a $nmcount -eq 3 -a $rmcount -eq 1 ]; then
echo "--------hadoop had started!--------"
echo "NameNode'number:$nncount [need 2]"
echo "DataNode'number:$dncount [need 3]"
echo "NodeManager'number:$nmcount [need 3]"
echo "ResourceManager'number:$rmcount [need 1]"
else
echo "--------hadoop had no started or hadoop start faild!--------"
echo "NameNode'number:$nncount [need 2]"
echo "DataNode'number:$dncount [need 3]"
echo "NodeManager'number:$nmcount [need 3]"
echo "ResourceManager'number:$rmcount [need 1]"
mysh jps
exit
fi
}
# 检查zookeeper是否启动
function check_zookeeper(){
zkcount=`mysh jps |grep QuorumPeerMain |wc -l`
if [ $zkcount -eq 3 ]; then
echo "--------zookeeper had started!--------"
echo `mysh jps |grep QuorumPeerMain`
else
echo "--------zookeeper had no started or zookeeper start faild!--------"
echo `mysh jps |grep QuorumPeerMain`
exit
fi
}
# 检查MySQL服务是否已经启动
function check_mysql(){
mysqlcount=`ps -e | grep mysqld |wc -l`
if [ $mysqlcount -ge 1 ]; then
echo "--------mysql had started [hadoop01]--------"
echo `ps -e | grep mysqld`
else
if [ $LINUX_VERSION -ge 8 ]; then
systemctl start mysqld.service
else
service mysqld start
fi
if [ $? -ne 0 ]; then
echo "--------mysql server start faild!!!--------"
echo `ps -e | grep mysqld`
exit
else
echo "--------mysql server start secced [hadoop01]--------"
echo `ps -e | grep mysqld`
fi
fi
}
if [ $# = 0 ]; then
print_usage
exit
fi
COMMAND=$1
case $COMMAND in
# usage flags
--help|-help|-h)
print_usage
exit
;;
jps)
for host in ${HOSTS[*]}; do
echo ------------------- $host --------------
ssh $user@$host "$JAVA_HOME/bin/jps"
done
;;
zk|zookeeper)
if [[ $# -lt 2 ]]; then
echo "zk lease args"
echo "please input ${1} start|stop"
exit
fi
if [[ $2 == "start" ]]; then
for i in ${HOSTS[*]}; do
echo ------------------- $2 $i zookeeper -------------------
ssh $i "source /etc/profile;$ZOOKEEPER_HOME/bin/zkServer.sh $2"
done
echo =====================================================
echo =====================================================
sleep 5
for j in ${HOSTS[*]}; do
echo ------------------- $j zookeeper status -------------------
ssh $j "source /etc/profile;$ZOOKEEPER_HOME/bin/zkServer.sh status"
done
elif [[ $2 == "stop" ]]; then
for i in ${HOSTS[*]}; do
echo ------------------- $2 $i zookeeper -------------------
ssh $i "source /etc/profile;$ZOOKEEPER_HOME/bin/zkServer.sh $2"
done
echo =====================================================
echo =====================================================
mysh jps
else
echo "please input ${1} start|stop";
exit;
fi
;;
hdp|hadoop)
if [[ $# -lt 2 ]]; then
echo hadoop lease args;
echo "please input ${1} start|stop";
exit;
fi
if [[ $2 == "start" ]]; then
# 检查zookeeper是否启动
check_zookeeper
for i in ${HOSTS[0]}; do
echo ------------------- $i 正在启动hdfs --------------
ssh $i "source /etc/profile;$HADOOP_HOME/sbin/start-dfs.sh"
done
for i in ${HOSTS[1]}; do
echo ------------------- $i 正在启动yarn --------------
ssh $i "source /etc/profile;$HADOOP_HOME/sbin/start-yarn.sh"
done
for i in ${HOSTS[2]}; do
echo ------------------- $i 正在启动JobHistory --------------
ssh $i "source /etc/profile;$HADOOP_HOME/sbin/mr-jobhistory-daemon.sh start historyserver"
done
echo =====================================================
echo =====================================================
sleep 5
check_hadoop
mysh jps
elif [[ $2 == "stop" ]]; then
for i in ${HOSTS[2]}; do
echo ------------------- $i 正在关闭JobHistory --------------
ssh $i "source /etc/profile;$HADOOP_HOME/sbin/mr-jobhistory-daemon.sh stop historyserver"
done
for i in ${HOSTS[1]}; do
echo ------------------- $i 正在关闭yarn --------------
ssh $i "source /etc/profile;$HADOOP_HOME/sbin/stop-yarn.sh"
done
for i in ${HOSTS[0]}; do
echo ------------------- $i 正在关闭hdfs --------------
ssh $i "source /etc/profile;$HADOOP_HOME/sbin/stop-dfs.sh"
done
echo =====================================================
echo =====================================================
mysh jps
else
echo "please input ${1} start|stop";
exit;
fi
;;
hive|hiveserver2|hs2)
if [[ $# -lt 2 ]]; then
echo "hiveserver2 lease args";
echo "please input ${1} start|stop";
exit;
fi
if [ $2 == "start" ]; then
# 检查MySQL服务是否已经启动
check_mysql
# 检测Hadoop是否启动
check_hadoop
echo "------------------- hadoop01 正在启动hiveserver2 --------------"
hiveserver2_count=`jps |grep RunJar |wc -l`
if [ $hiveserver2_count -ge 1 ]; then
jps |grep RunJar
echo "直接启动beeline"
beeline -u jdbc:hive2://hadoop01:10000/hivesql -nroot -proot
else
echo 'nohup hiveserver2 >/dev/null 2>&1 &'
nohup hiveserver2 >/dev/null 2>&1 &
if [ $? -eq 0 ]; then
sleep 20 # 这个一定要等20秒,以便hiveserver2完全启动......
jps |grep RunJar
echo "后启动hiveserver2"
beeline -u jdbc:hive2://hadoop01:10000/hivesql -nroot -proot
else
echo "-------------------hiveserver2 start faild!-------------------"
fi
fi
elif [ $2 == "stop" ]; then
echo "------------------- hadoop01 正在关闭hiveserver2 --------------"
hiveserver2_count=`jps |grep RunJar |wc -l`
if [ $hiveserver2_count -ge 1 ]; then
jps |grep RunJar
echo `jps |grep RunJar |awk -F" " {'print $1'}` |xargs kill -9
fi
mysh jps
else
echo "please input ${1} start|stop";
exit;
fi
;;
*)
print_usage
exit
;;
esac
2.集群配置文件:args_config.pro
LINUX_VERSION=8=
HOSTS=hadoop01 hadoop02 hadoop03=
ZOOKEEPER_HOME=/root/servers/zookeeper-3.5.5=
HADOOP_HOME=/root/servers/hadoop-2.8.5=
HIVE_HOME=/root/servers/hive-apache-2.3.6=
3.说明:
1.mysh 与 args_config.pro 在同一目录下
2.args_config.pro中第二个等号不能省略(否则得到文本后面会多一个换行符)