#!/bin/bash
HIVE_HOME='/opt/software/hive-3.1.3'
HIVE_LOG_HOME='/opt/software/hive-3.1.3/log'function checkLogDir {if [[ ! -e ${HIVE_LOG_HOME} ]]; thenecho "${HIVE_LOG_HOME} 目录不存在,正在创建。"mkdir -p ${HIVE_LOG_HOME}fi
}function checkHiveMetaStoreProgess {metastore=`ps -ef | grep -n HiveMetaStore | grep -v grep | awk '{print $2}'`if [[ ! -z ${metastore} ]]; thenreturn 1elsereturn 0fi
}function checkHiveServer2Progess {hiveserver2=`ps -ef | grep -n HiveServer2 | grep -v grep | awk '{print $2}'`if [[ ! -z ${hiveserver2} ]]; thenreturn 1elsereturn 0fi
}function stopProgess {checkHiveMetaStoreProgessmetastore=`ps -ef | grep -n HiveMetaStore | grep -v grep | awk '{print $2}'`if [[ ! -z ${metastore} ]]; thenecho "HIVE HiveMetaStore 正在停止..."kill ${metastore}elseecho "HIVE HiveMetaStore 服务未正常运行"ficheckHiveServer2Progesshiveserver2=`ps -ef | grep -n HiveServer2 | grep -v grep | awk '{print $2}'`if [[ ! -z ${hiveserver2} ]]; thenecho "HIVE HiveServer2 正在停止..."kill ${hiveserver2}elseecho "HIVE HiveServer2 服务未正常运行"fi
}function startProgess {checkLogDirif [[ $? -eq 0 ]]; thencheckHiveMetaStoreProgessif [[ $? -eq 0 ]]; thenecho "正在启动 HIVE MetaStore..."`nohup hive --service metastore > ${HIVE_LOG_HOME}/metastore_log 2>&1 &`elseecho "HIVE MetaStore 已经启动"ficheckHiveServer2Progessif [[ $? -eq 0 ]]; thenecho "正在启动 HIVE HiveServer2..."`nohup hive --service hiveserver2 > ${HIVE_LOG_HOME}/hiveserver2_log 2>&1 &`elseecho "HIVE HiveServer2 已经启动"fifi
}case ${1} instart )startProgess;;stop )stopProgess;;status )checkHiveMetaStoreProgessif [[ $? -eq 0 ]]; thenecho "HIVE MetaStore 未启动"elseecho "HIVE MetaStore 已经启动"ficheckHiveServer2Progessif [[ $? -eq 0 ]]; thenecho "HIVE HiveServer2 未启动"elseecho "HIVE HiveServer2 已经启动"fi;;* )echo "参数异常 hive-service.sh start|stop|status";;
esac