如何在Ubuntu 16.04中启动时启动Mosquitto MQTT代理


10

我正在尝试在Ubuntu 16.04上运行Mosquitto MQTT Broker。我有来自14.04的说明,但是由于16.04不再使用启动,所以我不知道如何转换为systemd。

以下是14.04的命令:

description "Mosquitto MQTT broker"
start on net-device-up
respawn
exec /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

编辑:

到目前为止,这就是我要做的。当我重新启动并运行时systemctl status mosquitto.service,显示mosquitto已加载但未激活:

[Unit]
Description=Mosquitto MQTT Broker

[Service]
WorkingDirectory=/usr/sbin/mosquitto
ExecStart=/etc/mosquitto/mosquitto.conf
Restart=always

[Install]
WantedBy=multi-user.target

编辑2:

sudo systemctl enable mosquitto.service。现在错误退出代码为:

Process: 1720 ExecStart=/etc/mosquitto/mosquitto.conf (code=exited, status=200/CHDIR)

Answers:


9

安装sudo apt install mosquitto16.04添加/etc/init.d/mosquitto脚本。systemd使用该脚本生成/run/systemd/generator.late/mosquitto.service,这就是它的意思:

# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/init.d/mosquitto
Description=LSB: mosquitto MQTT v3.1 message broker
Before=multi-user.target
Before=multi-user.target
Before=multi-user.target
Before=graphical.target
Before=shutdown.target
After=remote-fs.target
After=systemd-journald-dev-log.socket
Conflicts=shutdown.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/init.d/mosquitto start
ExecStop=/etc/init.d/mosquitto stop
ExecReload=/etc/init.d/mosquitto reload

看里面/etc/init.d/mosquitto告诉我们

/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf 默认已经运行

因此,只需将您的配置放入/etc/mosquitto/conf.d(由引​​用mosquitto.conf)并欣赏即可


作为常规的systemd服务,您可以:

  • systemctl status mosquitto 查看其当前状态
  • sudo systemctl (start|stop) mosquitto 更改当前会话的状态
  • sudo systemctl (enable|disable) mosquitto 更改启动行为(不影响当前状态)

该文件如何告诉您正在使用mosquitto -c option
Petrus Theron

@PetrusTheron就是/etc/init.d/mosquitto带有start参数的文件中发生的情况。
Mihail Malostanidis

/etc/mosquitto/conf.d目录中的我的配置似乎被忽略了。我该如何再生/run/systemd/generator.late/mosquitto.service
克里斯,

@chris可能未在其中引用它/etc/mosquitto/mosquitto.conf?里面有什么?
1:30的Mihail Malostanidis

0

您的单位文件ExecStart=WorkingDirectory=行是完全错误的。您是否要使用可执行文件(!)作为工作目录来启动配置文件?

如果我理解正确,正确的单位文件将是:

[Unit]
Description=Mosquitto MQTT Broker
After=network.target

[Service]
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=always

[Install]
WantedBy=multi-user.target

另外,请不要混淆启动和启用设备。阅读我对此的类似答案:


0

确保您正在运行蚊子:

ps -aux | grep mosquitto

而且你在default.conf中有这个:

listener 1883
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.