Hive操作脚本(hive.sh {start|stop|restart|status})

注意:此脚本安装于Hive服务端。

  1. cd /usr/local/bin
  2. sudo vi hive.sh

脚本内容如下:

  1. #!/bin/bash
  2. HIVE_LOG_DIR=/home/vagrant/modules/apache-hive-2.3.4-bin/logs
  3. mkdir -p $HIVE_LOG_DIR
  4. # 检查进程是否运行正常,参数1为进程名,参数2为进程端口
  5. function check_process()
  6. {
  7. pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
  8. ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
  9. echo $pid
  10. [[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
  11. }
  12. function hive_start()
  13. {
  14. metapid=$(check_process HiveMetastore 9083)
  15. cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
  16. cmd=$cmd" sleep 4; hdfs dfsadmin -safemode wait >/dev/null 2>&1"
  17. [ -z "$metapid" ] && eval $cmd || echo "Metastroe is running.(${HOSTNAME})"
  18. server2pid=$(check_process HiveServer2 10000)
  19. cmd="nohup hive --service hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
  20. [ -z "$server2pid" ] && eval $cmd || echo "HiveServer2 is running.(${HOSTNAME})"
  21. }
  22. function hive_stop()
  23. {
  24. metapid=$(check_process HiveMetastore 9083)
  25. [ "$metapid" ] && kill $metapid || echo "Metastore stop.(${HOSTNAME})"
  26. server2pid=$(check_process HiveServer2 10000)
  27. [ "$server2pid" ] && kill $server2pid || echo "HiveServer2 stop.(${HOSTNAME})"
  28. }
  29. function hive_status()
  30. {
  31. check_process HiveMetastore 9083 >/dev/null && echo "Metastore is running.(${HOSTNAME})" || echo "Metastore stop.(${HOSTNAME})"
  32. check_process HiveServer2 10000 >/dev/null && echo "HiveServer2 is running.(${HOSTNAME})" || echo "HiveServer2 stop.(${HOSTNAME})"
  33. }
  34. case $1 in
  35. "start")
  36. hive_start
  37. sleep 3
  38. hive_status
  39. ;;
  40. "stop")
  41. hive_stop
  42. sleep 5
  43. hive_status
  44. ;;
  45. "restart")
  46. hive_stop
  47. sleep 3
  48. hive_start
  49. sleep 3
  50. hive_status
  51. ;;
  52. "status")
  53. hive_status
  54. ;;
  55. *)
  56. echo Invalid Args!
  57. echo 'Usage: '$(basename $0)' start|stop|restart|status'
  58. ;;
  59. esac

设置脚本执行权限:

  1. sudo chmod +x hive.sh
  2. sudo chown vagrant:vagrant hive.sh

操作指引:

  1. # 启动
  2. hive.sh start
  3. # 关闭
  4. hive.sh stop
  5. # 重启
  6. hive.sh restart
  7. # 状态
  8. hive.sh status

Hive服务启动脚本(_start-hive-server)_

  1. cd /usr/local/bin
  2. sudo vi start-hive-server

脚本内容如下:

  1. #!/bin/bash
  2. echo "start hive metastore & hiveserver2-------------------------------------------------"
  3. for i in bigdata-node1
  4. do
  5. ssh $i "source /etc/profile;/usr/local/bin/hive.sh start"
  6. done
  7. sleep 3s
  8. exit 0

设置脚本执行权限:

  1. sudo chmod +x start-hive-server
  2. sudo chown vagrant:vagrant start-hive-server

Hive服务停止脚本(_stop-hive-server)_

  1. cd /usr/local/bin
  2. sudo vi stop-hive-server

脚本内容如下:

  1. #!/bin/bash
  2. echo "stop hive metastore & hiveserver2-------------------------------------------------"
  3. for i in bigdata-node1
  4. do
  5. ssh $i "source /etc/profile;/usr/local/bin/hive.sh stop"
  6. done
  7. sleep 3s
  8. exit 0

设置脚本执行权限:

  1. sudo chmod +x stop-hive-server
  2. sudo chown vagrant:vagrant stop-hive-server

Hive服务状态查看脚本(_status-hive-server)_

  1. cd /usr/local/bin
  2. sudo vi status-hive-server

脚本内容如下:

  1. #!/bin/bash
  2. echo "status hive metastore & hiveserver2-------------------------------------------------"
  3. for i in bigdata-node1
  4. do
  5. ssh $i "source /etc/profile;/usr/local/bin/hive.sh status"
  6. done
  7. sleep 3s
  8. exit 0

设置脚本执行权限:

  1. sudo chmod +x status-hive-server
  2. sudo chown vagrant:vagrant status-hive-server

分发(选做):

  1. # 使用root账户分发至其他节点
  2. sudo scp -r /usr/local/bin/*-hive-server root@bigdata-node2:/usr/local/bin/
  3. sudo scp -r /usr/local/bin/*-hive-server root@bigdata-node3:/usr/local/bin/