init.script 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/sh
  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. . /etc/rc.d/init.d/functions
  11. RETVAL=0
  12. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  13. DESC="EMQX, a distributed, massively scalable, highly extensible MQTT message broker written in Erlang/OTP"
  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. [ -r /etc/sysconfig/$NAME ] && . /etc/sysconfig/$NAME
  25. # `service` strips all environmental VARS so
  26. # if no HOME was set in /etc/sysconfig/$NAME then set one here
  27. # to the data directory for erlexec's sake
  28. if [ -z "$HOME" ]; then
  29. export HOME=
  30. fi
  31. status -p $pidfile -l $(basename $lockfile) $NAME >/dev/null 2>&1
  32. running=$?
  33. find_pid() {
  34. ps ax | grep -E "\-progname\s+$NAME\s" | awk '{print $1}'
  35. }
  36. check_pid_status() {
  37. local pid="$(find_pid)"
  38. if [ "$pid" = "" ]; then
  39. # prog not running?
  40. return 1
  41. else
  42. # running
  43. return 0
  44. fi
  45. }
  46. start() {
  47. # Start daemons.
  48. echo -n $"Starting emqx: "
  49. $DAEMON start
  50. RETVAL=$?
  51. if [ $RETVAL -eq 0 ]; then
  52. touch $lockfile
  53. find_pid > $pidfile
  54. success
  55. else
  56. failure $"$NAME start"
  57. fi
  58. echo
  59. return $RETVAL
  60. }
  61. stop() {
  62. # Stop daemon.
  63. echo -n $"Shutting down emqx: "
  64. $DAEMON stop 2>/dev/null
  65. for n in $(seq 1 10); do
  66. sleep 1
  67. check_pid_status
  68. RETVAL=$?
  69. if [ $RETVAL -eq 1 ]; then
  70. break
  71. fi
  72. done
  73. if [ $RETVAL -eq 1 ]; then
  74. rm -f $lockfile $pidfile
  75. success
  76. echo && return 0
  77. else
  78. failure $"$NAME stop"
  79. echo && return 1
  80. fi
  81. }
  82. hardstop() {
  83. echo -n $"Shutting down $NAME: "
  84. su - emqx -c "ps -ef | grep -E '\-progname\s+$NAME\s' | awk '{print \$2}' | xargs kill -9"
  85. for n in $(seq 1 10); do
  86. sleep 1
  87. check_pid_status
  88. RETVAL=$?
  89. if [ $RETVAL -eq 1 ]; then
  90. break
  91. fi
  92. done
  93. if [ $RETVAL -eq 1 ]; then
  94. rm -f $lockfile $pidfile
  95. success
  96. echo && return 0
  97. else
  98. failure $"$NAME hardstop"
  99. echo && return 1
  100. fi
  101. }
  102. # See how we were called.
  103. case "$1" in
  104. start)
  105. [ $running -eq 0 ] && exit 0
  106. start
  107. ;;
  108. stop)
  109. stop
  110. ;;
  111. restart|force-reload)
  112. [ $running -eq 0 ] && stop
  113. start
  114. ;;
  115. hardstop)
  116. [ $running -eq 0 ] || exit 0
  117. hardstop
  118. ;;
  119. condrestart|try-restart)
  120. [ $running -eq 0 ] || exit 0
  121. restart
  122. ;;
  123. status)
  124. status -p $pidfile -l $(basename $lockfile) $NAME
  125. ;;
  126. ping)
  127. $DAEMON ping || exit $?
  128. ;;
  129. *)
  130. echo $"Usage: $0 {start|stop|restart|force-reload|hardstop|condrestart|try-restart|status|ping}"
  131. exit 1
  132. esac
  133. exit $?