init.script 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env bash
  2. #
  3. # emqx
  4. #
  5. # chkconfig: 2345 80 30
  6. # description: EMQX, a distributed, massively scalable, highly extensible MQTT message broker written in Erlang/OTP
  7. # processname: beam
  8. #
  9. # Source function library.
  10. # shellcheck disable=SC1091
  11. . /etc/rc.d/init.d/functions
  12. RETVAL=0
  13. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  14. NAME=emqx
  15. DAEMON=/usr/bin/$NAME
  16. lockfile=/var/lock/subsys/$NAME
  17. mkdir -p /var/run/$NAME
  18. pidfile=/var/run/$NAME/$NAME.pid
  19. # Check for script, config and data dirs
  20. [ -x /usr/bin/$NAME ] || exit 0
  21. [ -d /etc/$NAME ] || exit 0
  22. [ -d /var/lib/$NAME ] || exit 0
  23. # Read configuration variable file if it is present and readable
  24. # shellcheck disable=SC1090
  25. [ -r /etc/sysconfig/$NAME ] && . /etc/sysconfig/$NAME
  26. # `service` strips all environmental VARS so
  27. # if no HOME was set in /etc/sysconfig/$NAME then set one here
  28. # to the data directory for erlexec's sake
  29. if [ -z "$HOME" ]; then
  30. export HOME=
  31. fi
  32. status -p $pidfile -l "$(basename $lockfile)" $NAME >/dev/null 2>&1
  33. running=$?
  34. find_pid() {
  35. # shellcheck disable=SC2009 # pgrep does not support Extended Regular Expressions
  36. ps ax | grep -E "\-progname\s+$NAME\s" | awk '{print $1}'
  37. }
  38. check_pid_status() {
  39. local pid
  40. pid="$(find_pid)"
  41. if [ "$pid" = "" ]; then
  42. # prog not running?
  43. return 1
  44. else
  45. # running
  46. return 0
  47. fi
  48. }
  49. start() {
  50. # Start daemons.
  51. echo -n $"Starting emqx: "
  52. $DAEMON start
  53. RETVAL=$?
  54. if [ $RETVAL -eq 0 ]; then
  55. touch $lockfile
  56. find_pid > $pidfile
  57. success
  58. else
  59. failure $"$NAME start"
  60. fi
  61. echo
  62. return $RETVAL
  63. }
  64. stop() {
  65. # Stop daemon.
  66. echo -n $"Shutting down emqx: "
  67. $DAEMON stop 2>/dev/null
  68. for _ in $(seq 1 10); do
  69. sleep 1
  70. check_pid_status
  71. RETVAL=$?
  72. if [ $RETVAL -eq 1 ]; then
  73. break
  74. fi
  75. done
  76. if [ $RETVAL -eq 1 ]; then
  77. rm -f $lockfile $pidfile
  78. success
  79. echo && return 0
  80. else
  81. failure $"$NAME stop"
  82. echo && return 1
  83. fi
  84. }
  85. hardstop() {
  86. echo -n $"Shutting down $NAME: "
  87. su - emqx -c "ps -ef | grep -E '\-progname\s+$NAME\s' | awk '{print \$2}' | xargs kill -9"
  88. for _ in $(seq 1 10); do
  89. sleep 1
  90. check_pid_status
  91. RETVAL=$?
  92. if [ $RETVAL -eq 1 ]; then
  93. break
  94. fi
  95. done
  96. if [ $RETVAL -eq 1 ]; then
  97. rm -f $lockfile $pidfile
  98. success
  99. echo && return 0
  100. else
  101. failure $"$NAME hardstop"
  102. echo && return 1
  103. fi
  104. }
  105. # See how we were called.
  106. case "$1" in
  107. start)
  108. [ $running -eq 0 ] && exit 0
  109. start
  110. ;;
  111. stop)
  112. stop
  113. ;;
  114. restart|force-reload)
  115. [ $running -eq 0 ] && stop
  116. start
  117. ;;
  118. hardstop)
  119. [ $running -eq 0 ] || exit 0
  120. hardstop
  121. ;;
  122. condrestart|try-restart)
  123. [ $running -eq 0 ] || exit 0
  124. restart
  125. ;;
  126. status)
  127. status -p $pidfile -l "$(basename $lockfile)" $NAME
  128. ;;
  129. ping)
  130. $DAEMON ping || exit $?
  131. ;;
  132. *)
  133. echo $"Usage: $0 {start|stop|restart|force-reload|hardstop|condrestart|try-restart|status|ping}"
  134. exit 1
  135. esac
  136. exit $?