Emqx: Systemd.service

Created on 1 Oct 2016  ยท  4Comments  ยท  Source: emqx/emqx

Hello

i'm trying to make autoboot service on Debian
copied files to /opt/emqttd
and here is emqttd.service at /etc/system/systemd:

Path: /etc/systemd/system/emqttd.service

Server path: /opt/emqttd/bin/emqttd

[Unit]
Description=EMQTTD mqtt broker
After=network.target

Requires=After=mysql.service # Requires the mysql service to run first

[Service]
ExecStart=/opt/emqttd/bin/emqttd start
Restart=always
RestartSec=5 # Restart service after 10 seconds if node service crashes
StandardOutput=syslog # Output to syslog
StandardError=syslog # Output to syslog
SyslogIdentifier=emqttd

User=

Group=

Environment=NODE_ENV=production PORT=1337
Type=forking

[Install]
WantedBy=multi-user.target

But every start i get error

โ— emqttd.service - EMQTTD mqtt broker
Loaded: loaded (/etc/systemd/system/emqttd.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since ะกะฑ 2016-10-01 18:10:23 VLAT; 103ms ago
Process: 27305 ExecStart=/opt/emqttd/bin/emqttd start (code=exited, status=0/SUCCESS)
Main PID: 27331 (code=exited, status=1/FAILURE)

ะพะบั‚ 01 18:10:23 debian systemd[1]: emqttd.service: Unit entered failed state.
ะพะบั‚ 01 18:10:23 debian systemd[1]: emqttd.service: Failed with result 'exit-code'.
ะพะบั‚ 01 18:10:23 debian systemd[1]: emqttd.service: Service hold-off time over, scheduling restart.
ะพะบั‚ 01 18:10:23 debian systemd[1]: Stopped EMQTTD mqtt broker.
ะพะบั‚ 01 18:10:23 debian systemd[1]: emqttd.service: Start request repeated too quickly.
ะพะบั‚ 01 18:10:23 debian systemd[1]: Failed to start EMQTTD mqtt broker.

please help to make service file?
thank you

Enhancement Task

Most helpful comment

I have a emqttd.service file that works for me with emqttd 2.0.5 on Ubuntu 16.04
As did the OP, I copied the emqttd directory to /opt. I also created a user/group pair called emqttd (without shell access).

My emqttd.service file ('/usr/lib/systemd/system/emqttd.service') is:

[Unit]
Description=emqttd - Erlang MQTT broker
Documentation=http://emqtt.io/docs/v2/
Wants=network-online.target
After=network-online.target

[Service]
#emqttd can't be tracked with a single PID as it starts a number of sub-processes
#so we treat it as a one-shot, per example 3 in https://www.freedesktop.org/software/systemd/man/systemd.service.html
#systemd doesn't recognise a primary process (so there can be no auto-restart after failure)
#and the ExecStart and ExecStop directives refer to brief program runs that manage the sub-processes
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=-/etc/default/emqttd # the prefix hyphen means that this is an optional file
User=emqttd
Group=emqttd
WorkingDirectory=/opt/emqttd
ExecStart=/opt/emqttd/bin/emqttd start
ExecStop=/opt/emqttd/bin/emqttd stop

[Install]
WantedBy=multi-user.target

ymmv

Note that if #829 results in a visible PID file, the above could be changed to Type=forking with the PID file identified via a PIDfile=xxx directive. that would enable auto restart and similar features.
Brian

All 4 comments

I found a workaround for this. This is my systemd unit:

[Unit]
Description=EMQ Broker
After=syslog.target
After=network.target

[Service]
Type=forking
TimeoutSec=60
Restart=always
RestartSec=10
Environment=HOME=/root
ExecStart=/opt/emqttd/bin/emqttd start
ExecStop=/opt/emqttd/bin/emqttd stop

Also, you need to modify the emqttd script inside your bin/ folder. _Note that I'm on version 1.1.2._ On line 246, I've added some flags, resulting in this line:
-args_file $RUNNER_ETC_DIR/vm.args -detached -noinput -noshell -- ${1+"$@"}".

I think this issue is related to ./emqttd console being an interactive session, breaking systemd's ability to check the error codes.

@emqplus, this project needs to have a proper systemd unit-file.

I use this version (change some lines from manual):

export HOME=/root

start() {
echo "starting emqttd..."
/opt/emqttd/bin/emqttd start
}

stop() {
echo "stopping emqttd..."
/opt/emqttd/bin/emqttd stop

I have a emqttd.service file that works for me with emqttd 2.0.5 on Ubuntu 16.04
As did the OP, I copied the emqttd directory to /opt. I also created a user/group pair called emqttd (without shell access).

My emqttd.service file ('/usr/lib/systemd/system/emqttd.service') is:

[Unit]
Description=emqttd - Erlang MQTT broker
Documentation=http://emqtt.io/docs/v2/
Wants=network-online.target
After=network-online.target

[Service]
#emqttd can't be tracked with a single PID as it starts a number of sub-processes
#so we treat it as a one-shot, per example 3 in https://www.freedesktop.org/software/systemd/man/systemd.service.html
#systemd doesn't recognise a primary process (so there can be no auto-restart after failure)
#and the ExecStart and ExecStop directives refer to brief program runs that manage the sub-processes
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=-/etc/default/emqttd # the prefix hyphen means that this is an optional file
User=emqttd
Group=emqttd
WorkingDirectory=/opt/emqttd
ExecStart=/opt/emqttd/bin/emqttd start
ExecStop=/opt/emqttd/bin/emqttd stop

[Install]
WantedBy=multi-user.target

ymmv

Note that if #829 results in a visible PID file, the above could be changed to Type=forking with the PID file identified via a PIDfile=xxx directive. that would enable auto restart and similar features.
Brian

The systemd.service file has been released since version 2.2:

https://github.com/emqtt/emq-package/blob/master/rpm/emqttd.service

Was this page helpful?
0 / 5 - 0 ratings

Related issues

heyoka picture heyoka  ยท  3Comments

viest picture viest  ยท  5Comments

rootqa picture rootqa  ยท  3Comments

kenpeter picture kenpeter  ยท  6Comments

jemc picture jemc  ยท  5Comments