init.script 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #! /bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: emqx
  4. # Required-Start: $remote_fs $syslog
  5. # Required-Stop: $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Erlang MQTT Broker
  9. # Description: EMQX, a distributed, massively scalable, highly extensible MQTT message broker written in Erlang/OT
  10. ### END INIT INFO
  11. NAME=emqx
  12. DAEMON=/usr/bin/$NAME
  13. SCRIPTNAME=/etc/init.d/$NAME
  14. # Read configuration variable file if it is present
  15. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  16. # Load the VERBOSE setting and other rcS variables
  17. . /lib/init/vars.sh
  18. . /lib/lsb/init-functions
  19. # `service` strips all environmental VARS so
  20. # if no HOME was set in /etc/default/$NAME then set one here
  21. # to the data directory for erlexec's sake
  22. if [ -z "$HOME" ]; then
  23. export HOME=/var/lib/emqx
  24. fi
  25. #
  26. # Function that starts the daemon/service
  27. #
  28. do_start()
  29. {
  30. # Return
  31. # 0 if daemon has been started
  32. # 1 if daemon was already running
  33. # 2 if daemon could not be started
  34. # Startup with the appropriate user
  35. start-stop-daemon --start \
  36. --name emqx \
  37. --user emqx \
  38. --exec $DAEMON -- start \
  39. || return 2
  40. }
  41. #
  42. # Function that stops the daemon/service
  43. #
  44. do_stop()
  45. {
  46. # Identify the erts directory
  47. ERTS_PATH=`$DAEMON ertspath`
  48. # Attempt a clean shutdown.
  49. $DAEMON stop
  50. # waiting stop done sleep 5
  51. sleep 5
  52. # Return
  53. # 0 if daemon has been stopped
  54. # 1 if daemon was already stopped
  55. # 2 if daemon could not be stopped
  56. # other if a failure occurred
  57. # Make sure it's down by using a more direct approach
  58. start-stop-daemon --stop \
  59. --quiet \
  60. --retry=TERM/30/KILL/5 \
  61. --user emqx \
  62. --exec $ERTS_PATH/run_erl
  63. return $?
  64. }
  65. #
  66. # Function that graceful reload the daemon/service
  67. #
  68. do_reload() {
  69. # Restart the VM without exiting the process
  70. $DAEMON restart && return $? || return 2
  71. }
  72. # Checks the status of a node
  73. do_status() {
  74. $DAEMON ping && echo $"$NAME is running" && return 0
  75. echo $"$NAME is stopped" && return 2
  76. }
  77. case "$1" in
  78. start)
  79. log_daemon_msg "Starting $NAME"
  80. $DAEMON ping >/dev/null 2>&1 && echo $"$NAME is already running" && exit 0
  81. do_start
  82. case "$?" in
  83. 0|1) log_end_msg 0 ;;
  84. 2) log_end_msg 1
  85. exit 1
  86. ;;
  87. esac
  88. ;;
  89. stop)
  90. log_daemon_msg "Stopping $NAME"
  91. do_stop
  92. case "$?" in
  93. 0|1) log_end_msg 0 ;;
  94. 2) log_end_msg 1
  95. exit 1
  96. ;;
  97. esac
  98. ;;
  99. ping)
  100. # See if the VM is alive
  101. $DAEMON ping || exit $?
  102. ;;
  103. reload|force-reload)
  104. log_daemon_msg "Reloading $NAME"
  105. do_reload
  106. ES=$?
  107. log_end_msg $ES
  108. exit $ES
  109. ;;
  110. restart)
  111. log_daemon_msg "Restarting $NAME"
  112. do_stop
  113. case "$?" in
  114. 0|1)
  115. do_start
  116. case "$?" in
  117. 0) log_end_msg 0 ;;
  118. 1) log_end_msg 1 && exit 1 ;; # Old process is still running
  119. *) log_end_msg 1 && exit 1 ;; # Failed to start
  120. esac
  121. ;;
  122. *)
  123. # Failed to stop
  124. log_end_msg 1 && exit 1
  125. ;;
  126. esac
  127. ;;
  128. status)
  129. do_status && exit 0 || exit $?
  130. ;;
  131. *)
  132. echo "Usage: $SCRIPTNAME {start|stop|ping|restart|force-reload|status}" >&2
  133. exit 3
  134. ;;
  135. esac