04elasticsearch.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. #set -x
  3. set -e
  4. echoPrefix="\033[36m+"
  5. echoSuffix="\033[0m"
  6. installPath=/opt/elasticsearch/elasticsearch-7.17.5
  7. serverName=test_elasticsearch
  8. cmd=$1
  9. startServer() {
  10. pid=$1
  11. if [ -z $pid ];then
  12. echo -e "$echoPrefix nohup $installPath/bin/elasticsearch >>/dev/null 2>&1 & $echoSuffix"
  13. nohup $installPath/bin/elasticsearch >>/dev/null 2>&1 &
  14. echo "$serverName 启动成功!"
  15. else
  16. echo "$serverName 正在运行..."
  17. fi
  18. echo "日志目录: cd $installPath/logs"
  19. }
  20. stopServer() {
  21. pid=$1
  22. if [ -z $pid ];then
  23. echo "$serverName 未运行"
  24. else
  25. echo -e "$echoPrefix ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print \$2}' | xargs kill $echoSuffix"
  26. ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}' | xargs kill
  27. echo "$serverName 已停止!"
  28. fi
  29. }
  30. restartServer() {
  31. pid=$1
  32. if [ ! -z $pid ];then
  33. stopServer $pid
  34. sleep 3s
  35. fi
  36. startServer
  37. }
  38. pid=`ps -ef | grep "DappName=${serverName}" | grep -v 'grep' | awk '{print $2}'`
  39. # 操作参数: restart start stop kill
  40. if [ "$cmd" == "restart" ];then
  41. restartServer $pid
  42. elif [ "$cmd" == "start" ];then
  43. startServer $pid
  44. elif [ "$cmd" == "stop" ] || [ "$cmd" == "kill" ];then
  45. stopServer $pid
  46. else
  47. if [ -z $pid ];then
  48. echo "$serverName 未运行 | 输入操作参数: restart start stop kill"
  49. echo "日志目录: cd $installPath/logs"
  50. else
  51. echo "输入操作参数: restart start stop kill"
  52. echo "pid=$pid | $serverName 正在运行... | 日志目录: cd $installPath/logs"
  53. fi
  54. fi
  55. # bash <(curl -s http://git.yvanui.com/lizhiwei/jztd-deploy/raw/master/00base/04elasticsearch.sh) [cmd]