From 14ecfce69f906f4e6f119fd0b8cf1c1e9651d50e Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Thu, 6 Apr 2023 16:07:42 +0800 Subject: [PATCH 1/2] fix: systemd stop don't stop port in the desired order --- apps/emqx_machine/src/emqx_machine_terminator.erl | 7 ++++--- deploy/packages/emqx.service | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/emqx_machine/src/emqx_machine_terminator.erl b/apps/emqx_machine/src/emqx_machine_terminator.erl index 7120cc19b..77c53a64d 100644 --- a/apps/emqx_machine/src/emqx_machine_terminator.erl +++ b/apps/emqx_machine/src/emqx_machine_terminator.erl @@ -41,7 +41,7 @@ -define(DO_IT, graceful_shutdown). %% @doc This API is called to shutdown the Erlang VM by RPC call from remote shell node. -%% The shutown of apps is delegated to a to a process instead of doing it in the RPC spawned +%% The shutdown of apps is delegated to a to a process instead of doing it in the RPC spawned %% process which has a remote group leader. start_link() -> {ok, _} = gen_server:start_link({local, ?TERMINATOR}, ?MODULE, [], []). @@ -87,8 +87,9 @@ handle_cast(_Cast, State) -> handle_call(?DO_IT, _From, State) -> try - emqx_machine_boot:stop_apps(), - emqx_machine_boot:stop_port_apps() + %% stop port apps before stopping other apps. + emqx_machine_boot:stop_port_apps(), + emqx_machine_boot:stop_apps() catch C:E:St -> Apps = [element(1, A) || A <- application:which_applications()], diff --git a/deploy/packages/emqx.service b/deploy/packages/emqx.service index 2dbe550bc..9982c6ef5 100644 --- a/deploy/packages/emqx.service +++ b/deploy/packages/emqx.service @@ -22,8 +22,10 @@ LimitNOFILE=1048576 # ExecStop is commented out so systemd will send a SIGTERM when 'systemctl stop'. # SIGTERM is handled by EMQX and it then performs a graceful shutdown -# It's better than command 'emqx stop' because it needs to ping the node -# ExecStop=/bin/bash /usr/bin/emqx stop +# emqx stop will ping node, always return 0 to make sure next command will be executed +ExecStop=/bin/bash -c '/usr/bin/emqx stop; exit 0' +# If the process is still running, force kill it +ExecStop=/bin/sh -c 'if [ ps -p $MAINPID >/dev/null 2>&1 ]; then /bin/kill -15 $MAINPID; fi' # Wait long enough before force kill for graceful shutdown TimeoutStopSec=120s From e3d5df37f464f2b75183b6f10fe862659d13244b Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Mon, 8 May 2023 15:44:01 +0800 Subject: [PATCH 2/2] chore: replace /bin/sh with /bin/bash --- deploy/packages/emqx.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy/packages/emqx.service b/deploy/packages/emqx.service index 9982c6ef5..ce342b632 100644 --- a/deploy/packages/emqx.service +++ b/deploy/packages/emqx.service @@ -25,7 +25,7 @@ LimitNOFILE=1048576 # emqx stop will ping node, always return 0 to make sure next command will be executed ExecStop=/bin/bash -c '/usr/bin/emqx stop; exit 0' # If the process is still running, force kill it -ExecStop=/bin/sh -c 'if [ ps -p $MAINPID >/dev/null 2>&1 ]; then /bin/kill -15 $MAINPID; fi' +ExecStop=/bin/bash -c 'if [ ps -p $MAINPID >/dev/null 2>&1 ]; then kill -15 $MAINPID; fi' # Wait long enough before force kill for graceful shutdown TimeoutStopSec=120s