1)、編輯hive啟停shell腳本,腳本如下(說明:腳本別忘記了用戶需要執行權限)
#!/bin/sh
if [ $# -ne 2 ] ;then
echo "please input two params,first is (metastore|hiveserver2),second is (start|stop)"
exit 0
fi
if [ "$1" == "metastore" ] ; then
if [ "$2" == "start" ] ; then
cd /var/log/hive
echo "now is start metastore"
nohup /usr/local/hive/bin/hive --service metastore &
exit 0
elif [ "$2" == "stop" ] ; then
metastore_pid=`ps -ef|grep "HiveMetaStore"|grep "JAVA"|awk '{print $2}'`
kill ${metastore_pid}
echo "-------metastore has stop"
exit 0
else
echo "second param please input 'start' or 'stop'"
exit 0
fi
elif [ "$1" == "hiveserver2" ] ; then
if [ "$2" == "start" ] ; then
cd /var/log/hive
echo "now is start hiveserver2"
nohup /usr/local/hive/bin/hive --service hiveserver2 &
exit 0
elif [ "$2" == "stop" ] ; then
hiveserver2_pid=`ps -ef|grep "HiveServer2"|grep "java"|awk '{print $2}'`
kill ${hiveserver2_pid}
echo "-------hiveserver has stop"
exit 0
else
echo "second param please input 'start' or 'stop'"
exit 0
fi
else
echo "first param please input 'metastore' or 'hiveserver2'"
fi
2)、在/usr/lib/systemd/system創建對應的metastore和hiveserver啟停服務hive-hivesver2和hive-metastore是systemctl的啟停名稱
hive-hiveserver2.service腳本內容:
[Unit]
Description=metastore
Wants=network-online.target
After=network-online.target
[Service]
Type=forking
User=hive
Group=hadoop
Environment="HADOOP_HOME=/usr/local/hadoop3" #說明:這個環境變量得加上,否則會報找不到這個環境變量,雖然/usr/local/hive/conf/hive_service.sh hiveserver2 start能正常啟動,但是使用systemctl啟動就會找不到這個環境變量
ExecStart=/usr/local/hive/conf/hive_service.sh hiveserver2 start
ExecStop=/usr/local/hive/conf/hive_service.sh hiveserver2 stop
Restart=no
[Install]
WantedBy=multi-user.target
hive-metastore.service腳本內容:
[Unit]
Description=metastore
Wants=network-online.target
After=network-online.target
[Service]
Type=forking
User=hive
Group=hadoop
Environment="HADOOP_HOME=/usr/local/hadoop3"
ExecStart=/usr/local/hive/conf/hive_service.sh metastore start
ExecStop=/usr/local/hive/conf/hive_service.sh metastore stop
Restart=no
[Install]
WantedBy=multi-user.target
說明:每次修改service腳本之后要執行systemctl daemon-reload,否則修改的腳本不會被加載
3)、啟動服務 systemctl start hive-metastore
4)、停止服務systemctl stop hive-metastore