![如何在 Ubuntu 16.04 启动时启动 Mosquitto MQTT 代理](https://linux22.com/image/1088092/%E5%A6%82%E4%BD%95%E5%9C%A8%20Ubuntu%2016.04%20%E5%90%AF%E5%8A%A8%E6%97%B6%E5%90%AF%E5%8A%A8%20Mosquitto%20MQTT%20%E4%BB%A3%E7%90%86.png)
我正在尝试在 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)
答案1
安装sudo apt install mosquitto
在上16.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
改变启动行为(不影响当前状态)
答案2
确保 mosquitto 正在运行:
ps -aux | grep mosquitto
你有这个默认配置文件:
listener 1883
答案3
您的单元文件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
另外,请不要混淆启动和启用设备。阅读我对此的类似回答:
答案4
就我而言,我需要使用以下命令启动服务:
sudo systemctl start mosquitto.service