Hive操作脚本(hive.sh {start|stop|restart|status})
注意:此脚本安装于Hive服务端。
cd /usr/local/bin
sudo vi hive.sh
脚本内容如下:
#!/bin/bash
HIVE_LOG_DIR=/home/vagrant/modules/apache-hive-2.3.4-bin/logs
mkdir -p $HIVE_LOG_DIR
# 检查进程是否运行正常,参数1为进程名,参数2为进程端口
function check_process()
{
pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
echo $pid
[[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
}
function hive_start()
{
metapid=$(check_process HiveMetastore 9083)
cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
cmd=$cmd" sleep 4; hdfs dfsadmin -safemode wait >/dev/null 2>&1"
[ -z "$metapid" ] && eval $cmd || echo "Metastroe is running.(${HOSTNAME})"
server2pid=$(check_process HiveServer2 10000)
cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
[ -z "$server2pid" ] && eval $cmd || echo "HiveServer2 is running.(${HOSTNAME})"
}
function hive_stop()
{
metapid=$(check_process HiveMetastore 9083)
[ "$metapid" ] && kill $metapid || echo "Metastore stop.(${HOSTNAME})"
server2pid=$(check_process HiveServer2 10000)
[ "$server2pid" ] && kill $server2pid || echo "HiveServer2 stop.(${HOSTNAME})"
}
function hive_status()
{
check_process HiveMetastore 9083 >/dev/null && echo "Metastore is running.(${HOSTNAME})" || echo "Metastore stop.(${HOSTNAME})"
check_process HiveServer2 10000 >/dev/null && echo "HiveServer2 is running.(${HOSTNAME})" || echo "HiveServer2 stop.(${HOSTNAME})"
}
case $1 in
"start")
hive_start
sleep 3
hive_status
;;
"stop")
hive_stop
sleep 5
hive_status
;;
"restart")
hive_stop
sleep 3
hive_start
sleep 3
hive_status
;;
"status")
hive_status
;;
*)
echo Invalid Args!
echo 'Usage: '$(basename $0)' start|stop|restart|status'
;;
esac
设置脚本执行权限:
sudo chmod +x hive.sh
sudo chown vagrant:vagrant hive.sh
操作指引:
# 启动
hive.sh start
# 关闭
hive.sh stop
# 重启
hive.sh restart
# 状态
hive.sh status
Hive服务启动脚本(_start-hive-server)_
cd /usr/local/bin
sudo vi start-hive-server
脚本内容如下:
#!/bin/bash
echo "start hive metastore & hiveserver2-------------------------------------------------"
for i in bigdata-node1
do
ssh $i "source /etc/profile;/usr/local/bin/hive.sh start"
done
sleep 3s
exit 0
设置脚本执行权限:
sudo chmod +x start-hive-server
sudo chown vagrant:vagrant start-hive-server
Hive服务停止脚本(_stop-hive-server)_
cd /usr/local/bin
sudo vi stop-hive-server
脚本内容如下:
#!/bin/bash
echo "stop hive metastore & hiveserver2-------------------------------------------------"
for i in bigdata-node1
do
ssh $i "source /etc/profile;/usr/local/bin/hive.sh stop"
done
sleep 3s
exit 0
设置脚本执行权限:
sudo chmod +x stop-hive-server
sudo chown vagrant:vagrant stop-hive-server
Hive服务状态查看脚本(_status-hive-server)_
cd /usr/local/bin
sudo vi status-hive-server
脚本内容如下:
#!/bin/bash
echo "status hive metastore & hiveserver2-------------------------------------------------"
for i in bigdata-node1
do
ssh $i "source /etc/profile;/usr/local/bin/hive.sh status"
done
sleep 3s
exit 0
设置脚本执行权限:
sudo chmod +x status-hive-server
sudo chown vagrant:vagrant status-hive-server
分发(选做):
# 使用root账户分发至其他节点
sudo scp -r /usr/local/bin/*-hive-server root@bigdata-node2:/usr/local/bin/
sudo scp -r /usr/local/bin/*-hive-server root@bigdata-node3:/usr/local/bin/