diff --git a/.ci/build_packages/tests.sh b/.ci/build_packages/tests.sh index 4211d1d8d..3b97c01b8 100755 --- a/.ci/build_packages/tests.sh +++ b/.ci/build_packages/tests.sh @@ -135,23 +135,6 @@ running_test(){ pytest -v /paho-mqtt-testing/interoperability/test_client/V5/test_connect.py::test_basic # shellcheck disable=SC2009 # pgrep does not support Extended Regular Expressions emqx stop || kill "$(ps -ef | grep -E '\-progname\s.+emqx\s' |awk '{print $2}')" - - if [ "$(sed -n '/^ID=/p' /etc/os-release | sed -r 's/ID=(.*)/\1/g' | sed 's/"//g')" = ubuntu ] \ - || [ "$(sed -n '/^ID=/p' /etc/os-release | sed -r 's/ID=(.*)/\1/g' | sed 's/"//g')" = debian ] ;then - service emqx start || ( tail /var/log/emqx/emqx.log.1 && exit 1 ) - IDLE_TIME=0 - while ! emqx_ctl status | grep -E 'Node\s.*@.*\sis\sstarted' - do - if [ $IDLE_TIME -gt 10 ] - then - echo "emqx service error" - exit 1 - fi - sleep 10 - IDLE_TIME=$((IDLE_TIME+1)) - done - service emqx stop - fi } relup_test(){ diff --git a/deploy/packages/deb/Makefile b/deploy/packages/deb/Makefile index 48124d780..1cfc4d514 100644 --- a/deploy/packages/deb/Makefile +++ b/deploy/packages/deb/Makefile @@ -13,6 +13,7 @@ TARGET_PKG := $(EMQX_NAME)-$(SYSTEM)-$(PKG_VSN)-$(ARCH) .PHONY: all all: | $(BUILT) cp -r debian $(SRCDIR)/ + cp emqx.service $(SRCDIR)/debian/${EMQX_NAME}.service sed -i "s##$(shell date -u '+%a, %d %b %Y %T %z')#g" $(SRCDIR)/debian/changelog sed -i "s##$(PKG_VSN)#g" $(SRCDIR)/debian/changelog sed -i "s/emqx-pkg/$(EMQX_NAME)-pkg/g" $(SRCDIR)/debian/rules; \ diff --git a/deploy/packages/deb/debian/init.script b/deploy/packages/deb/debian/init.script deleted file mode 100755 index 0fcafd1d2..000000000 --- a/deploy/packages/deb/debian/init.script +++ /dev/null @@ -1,150 +0,0 @@ -#! /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 - - diff --git a/deploy/packages/deb/debian/postinst b/deploy/packages/deb/debian/postinst index 3c114db64..69936cbe0 100755 --- a/deploy/packages/deb/debian/postinst +++ b/deploy/packages/deb/debian/postinst @@ -5,9 +5,6 @@ set -e -# install startup script -update-rc.d emqx defaults >/dev/null - # create group if ! getent group emqx >/dev/null; then addgroup --system emqx @@ -38,6 +35,12 @@ chmod -R 0755 /usr/lib/emqx/bin ln -s /usr/lib/emqx/bin/emqx /usr/bin/emqx ln -s /usr/lib/emqx/bin/emqx_ctl /usr/bin/emqx_ctl +if systemctl status --no-pager; then + systemctl enable emqx; +else + echo "systemd is not in use, skip enable emqx" +fi + case "$1" in configure) ;; diff --git a/deploy/packages/deb/debian/postrm b/deploy/packages/deb/debian/postrm index 9d6072f58..5df55135a 100755 --- a/deploy/packages/deb/debian/postrm +++ b/deploy/packages/deb/debian/postrm @@ -23,9 +23,6 @@ case "$1" in purge) rm -f /etc/default/emqx - # ensure we remove the rc.d scripts installed by postinst - update-rc.d emqx remove >/dev/null - if [ -d /var/lib/emqx ]; then rm -r /var/lib/emqx fi @@ -58,6 +55,11 @@ case "$1" in if [ -d /usr/lib/emqx ]; then rm -r /usr/lib/emqx fi + + if systemctl status --no-pager; then + systemctl disable emqx; + rm -f /etc/systemd/emqx.service + fi ;; remove) diff --git a/deploy/packages/deb/debian/rules b/deploy/packages/deb/debian/rules index 1a9fe98e2..9ca6c8e17 100755 --- a/deploy/packages/deb/debian/rules +++ b/deploy/packages/deb/debian/rules @@ -46,8 +46,10 @@ install: build cp -R releases debian/emqx/usr/lib/emqx cp -R etc/* debian/emqx/etc/emqx cp -R data/* debian/emqx/var/lib/emqx - install -m755 debian/init.script debian/emqx/etc/init.d/emqx - + + install -d debian/emqx/lib/systemd/system + install -p -m0644 debian/emqx.service debian/emqx/lib/systemd/system/emqx.service + dh_shlibdeps # We have nothing to do by default. diff --git a/deploy/packages/deb/emqx.service b/deploy/packages/deb/emqx.service new file mode 120000 index 000000000..2fc64d79d --- /dev/null +++ b/deploy/packages/deb/emqx.service @@ -0,0 +1 @@ +../emqx.service \ No newline at end of file diff --git a/deploy/packages/emqx.service b/deploy/packages/emqx.service new file mode 100644 index 000000000..ef9abfb01 --- /dev/null +++ b/deploy/packages/emqx.service @@ -0,0 +1,17 @@ +[Unit] +Description=emqx daemon +After=network.target + +[Service] +User=emqx +Group=emqx +Type=forking +Environment=HOME=/var/lib/emqx +ExecStart=/usr/bin/emqx start +LimitNOFILE=1048576 +ExecStop=/usr/bin/emqx stop +Restart=on-failure +RestartSec=5s + +[Install] +WantedBy=multi-user.target diff --git a/deploy/packages/rpm/emqx.service b/deploy/packages/rpm/emqx.service index 2acbc765a..ef9abfb01 100644 --- a/deploy/packages/rpm/emqx.service +++ b/deploy/packages/rpm/emqx.service @@ -7,9 +7,11 @@ User=emqx Group=emqx Type=forking Environment=HOME=/var/lib/emqx -ExecStart=/bin/sh /usr/bin/emqx start +ExecStart=/usr/bin/emqx start LimitNOFILE=1048576 -ExecStop=/bin/sh /usr/bin/emqx stop +ExecStop=/usr/bin/emqx stop +Restart=on-failure +RestartSec=5s [Install] WantedBy=multi-user.target