feat(systemd): start EMQX under systemd in foreground mode
This commit is contained in:
parent
4bc8f0d44b
commit
122e75c0b4
53
bin/emqx
53
bin/emqx
|
@ -540,6 +540,48 @@ latest_vm_args() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# backward compabible with 4.x
|
||||||
|
tr_log_to_env() {
|
||||||
|
local log_to=${EMQX_LOG__TO:-undefined}
|
||||||
|
# unset because it's unknown to 5.0
|
||||||
|
unset EMQX_LOG__TO
|
||||||
|
case "${log_to}" in
|
||||||
|
console)
|
||||||
|
export EMQX_LOG__CONSOLE_HANDLER__ENABLE='true'
|
||||||
|
export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='false'
|
||||||
|
;;
|
||||||
|
file)
|
||||||
|
export EMQX_LOG__CONSOLE_HANDLER__ENABLE='false'
|
||||||
|
export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='true'
|
||||||
|
;;
|
||||||
|
both)
|
||||||
|
export EMQX_LOG__CONSOLE_HANDLER__ENABLE='true'
|
||||||
|
export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE='true'
|
||||||
|
;;
|
||||||
|
default)
|
||||||
|
# want to use config file defaults, do nothing
|
||||||
|
;;
|
||||||
|
undefined)
|
||||||
|
# value not set, do nothing
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echoerr "Unknown environment value for EMQX_LOG__TO=${log_to} discarded"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
maybe_log_to_console() {
|
||||||
|
if [ "${EMQX_LOG__TO:-}" = 'default' ]; then
|
||||||
|
# want to use config file defaults, do nothing
|
||||||
|
unset EMQX_LOG__TO
|
||||||
|
else
|
||||||
|
tr_log_to_env
|
||||||
|
# ensure defaults
|
||||||
|
export EMQX_LOG__CONSOLE_HANDLER__ENABLE="${EMQX_LOG__CONSOLE_HANDLER__ENABLE:-true}"
|
||||||
|
export EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE="${EMQX_LOG__FILE_HANDLERS__DEFAULT__ENABLE:-false}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
## IS_BOOT_COMMAND is set for later to inspect node name and cookie from hocon config (or env variable)
|
## IS_BOOT_COMMAND is set for later to inspect node name and cookie from hocon config (or env variable)
|
||||||
case "${COMMAND}" in
|
case "${COMMAND}" in
|
||||||
start|console|console_clean|foreground)
|
start|console|console_clean|foreground)
|
||||||
|
@ -621,7 +663,7 @@ case "${COMMAND}" in
|
||||||
|
|
||||||
# this flag passes down to console mode
|
# this flag passes down to console mode
|
||||||
# so we know it's intended to be run in daemon mode
|
# so we know it's intended to be run in daemon mode
|
||||||
export _EMQX_START_MODE="$COMMAND"
|
export _EMQX_START_DAEMON_MODE=1
|
||||||
|
|
||||||
case "$COMMAND" in
|
case "$COMMAND" in
|
||||||
start)
|
start)
|
||||||
|
@ -764,8 +806,10 @@ case "${COMMAND}" in
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# set before generate_config
|
# set before generate_config
|
||||||
if [ "${_EMQX_START_MODE:-}" = '' ]; then
|
if [ "${_EMQX_START_DAEMON_MODE:-}" = 1 ]; then
|
||||||
export EMQX_LOG__CONSOLE_HANDLER__ENABLE="${EMQX_LOG__CONSOLE_HANDLER__ENABLE:-true}"
|
tr_log_to_env
|
||||||
|
else
|
||||||
|
maybe_log_to_console
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#generate app.config and vm.args
|
#generate app.config and vm.args
|
||||||
|
@ -815,8 +859,7 @@ case "${COMMAND}" in
|
||||||
# start up the release in the foreground for use by runit
|
# start up the release in the foreground for use by runit
|
||||||
# or other supervision services
|
# or other supervision services
|
||||||
|
|
||||||
# set before generate_config
|
maybe_log_to_console
|
||||||
export EMQX_LOG__CONSOLE_HANDLER__ENABLE="${EMQX_LOG__CONSOLE_HANDLER__ENABLE:-true}"
|
|
||||||
|
|
||||||
#generate app.config and vm.args
|
#generate app.config and vm.args
|
||||||
generate_config "$NAME_TYPE" "$NAME"
|
generate_config "$NAME_TYPE" "$NAME"
|
||||||
|
|
|
@ -5,13 +5,34 @@ After=network.target
|
||||||
[Service]
|
[Service]
|
||||||
User=emqx
|
User=emqx
|
||||||
Group=emqx
|
Group=emqx
|
||||||
Type=forking
|
|
||||||
|
# The ExecStart= is foreground, so 'simple' here
|
||||||
|
Type=simple
|
||||||
Environment=HOME=/var/lib/emqx
|
Environment=HOME=/var/lib/emqx
|
||||||
ExecStart=/usr/bin/emqx start
|
|
||||||
|
# Enable logging to file
|
||||||
|
Environment=EMQX_LOG__TO=default
|
||||||
|
|
||||||
|
# Start 'foregroun' but not 'start' (daemon) mode.
|
||||||
|
# Because systemd monitor/restarts 'simple' services
|
||||||
|
ExecStart=/usr/bin/emqx foreground
|
||||||
|
|
||||||
|
# Give EMQX enough file descriptors
|
||||||
LimitNOFILE=1048576
|
LimitNOFILE=1048576
|
||||||
ExecStop=/usr/bin/emqx stop
|
|
||||||
|
# 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=/usr/bin/emqx stop
|
||||||
|
|
||||||
|
# Wait long enough before force kill for graceful shutdown
|
||||||
|
TimeoutStopSec=120s
|
||||||
|
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
RestartSec=5s
|
|
||||||
|
# Do not restart immediately so the peer nodes in the cluster have
|
||||||
|
# enough time to handle the 'DOWN' events of this node
|
||||||
|
RestartSec=120s
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|
Loading…
Reference in New Issue