| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #! /bin/bash
- ### BEGIN INIT INFO
- # Provides: emqx
- # Required-Start: $remote_fs $syslog
- # Required-Stop: $remote_fs $syslog
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: Erlang MQTT Broker
- # Description: EMQX, a distributed, massively scalable, highly extensible MQTT message broker written in Erlang/OT
- ### END INIT INFO
- NAME=emqx
- DAEMON=/usr/bin/$NAME
- SCRIPTNAME=/etc/init.d/$NAME
- # Read configuration variable file if it is present
- [ -r /etc/default/$NAME ] && . /etc/default/$NAME
- # Load the VERBOSE setting and other rcS variables
- . /lib/init/vars.sh
- . /lib/lsb/init-functions
- # `service` strips all environmental VARS so
- # if no HOME was set in /etc/default/$NAME then set one here
- # to the data directory for erlexec's sake
- if [ -z "$HOME" ]; then
- export HOME=/var/lib/emqx
- fi
- #
- # Function that starts the daemon/service
- #
- do_start()
- {
- # Return
- # 0 if daemon has been started
- # 1 if daemon was already running
- # 2 if daemon could not be started
- # Startup with the appropriate user
- start-stop-daemon --start \
- --name emqx \
- --user emqx \
- --exec $DAEMON -- start \
- || return 2
- }
- #
- # Function that stops the daemon/service
- #
- do_stop()
- {
- # Identify the erts directory
- ERTS_PATH=`$DAEMON ertspath`
- # Attempt a clean shutdown.
- $DAEMON stop
- # waiting stop done sleep 5
- sleep 5
-
- # Return
- # 0 if daemon has been stopped
- # 1 if daemon was already stopped
- # 2 if daemon could not be stopped
- # other if a failure occurred
- # Make sure it's down by using a more direct approach
- start-stop-daemon --stop \
- --quiet \
- --retry=TERM/30/KILL/5 \
- --user emqx \
- --exec $ERTS_PATH/run_erl
- return $?
- }
- #
- # Function that graceful reload the daemon/service
- #
- do_reload() {
- # Restart the VM without exiting the process
- $DAEMON restart && return $? || return 2
- }
- # Checks the status of a node
- do_status() {
- $DAEMON ping && echo $"$NAME is running" && return 0
- echo $"$NAME is stopped" && return 2
- }
- case "$1" in
- start)
- log_daemon_msg "Starting $NAME"
- $DAEMON ping >/dev/null 2>&1 && echo $"$NAME is already running" && exit 0
- do_start
- case "$?" in
- 0|1) log_end_msg 0 ;;
- 2) log_end_msg 1
- exit 1
- ;;
- esac
- ;;
- stop)
- log_daemon_msg "Stopping $NAME"
- do_stop
- case "$?" in
- 0|1) log_end_msg 0 ;;
- 2) log_end_msg 1
- exit 1
- ;;
- esac
- ;;
- ping)
- # See if the VM is alive
- $DAEMON ping || exit $?
- ;;
- reload|force-reload)
- log_daemon_msg "Reloading $NAME"
- do_reload
- ES=$?
- log_end_msg $ES
- exit $ES
- ;;
- restart)
- log_daemon_msg "Restarting $NAME"
- do_stop
- case "$?" in
- 0|1)
- do_start
- case "$?" in
- 0) log_end_msg 0 ;;
- 1) log_end_msg 1 && exit 1 ;; # Old process is still running
- *) log_end_msg 1 && exit 1 ;; # Failed to start
- esac
- ;;
- *)
- # Failed to stop
- log_end_msg 1 && exit 1
- ;;
- esac
- ;;
- status)
- do_status && exit 0 || exit $?
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|ping|restart|force-reload|status}" >&2
- exit 3
- ;;
- esac
|