TransWikia.com

Recovering from USB dongle disconnection with systemd

Unix & Linux Asked by Gal on November 26, 2021

If this is not the right place to post a this question, please say.

I’m running zigbee2mqtt (Zigbee broker) on a USB dongle device (CC2531 with antenna, forgot the antenna model) and a Linux machine (KDE neon User Edition 5.19, based on Ubuntu 18.04).
Sometimes zigbee2mqtt stops working and I have to change the ownership of the device (with chown) to successfully run it again. So I put it in one script:

~/my_services/zigbee2mqtt_service $ cat z2m.sh 
sudo chown gal /dev/ttyACM0
cd /opt/zigbee2mqtt
npm start

and created a service in systemd to run the script:

~/my_services/zigbee2mqtt_service $ systemctl cat zigbee2mqtt.service 
# /etc/systemd/system/zigbee2mqtt.service
[Unit]
Description=zigbee2mqtt
After=network.target
[Service]
# ExecStart=/usr/local/bin/npm start
# WorkingDirectory=/opt/zigbee2mqtt
ExecStart=/home/gal/my_services/zigbee2mqtt_service/z2m.sh
StandardOutput=inherit
StandardError=inherit
Restart=always
User=gal
[Install]
WantedBy=multi-user.target

This used to work, but for some reason, now the service won’t start. I’m tried enabling it, starting manually, and to reload daemon. If I run the z2m.sh script manually it works well, but I’m working from a remote machine so I have to disconnect, which stops zigbee2mqtt.

It’d be great if you can point me to what to fix, or to a different way to recover from the ‘revoked ownership’.

Edit:
This is what I get when checking the service status:

~ $ systemctl status zigbee2mqtt.service 
● zigbee2mqtt.service - zigbee2mqtt
   Loaded: loaded (/etc/systemd/system/zigbee2mqtt.service; enabled; vendor pres
   Active: failed (Result: exit-code) since Mon 2020-07-20 11:00:57 IDT; 2 days 
  Process: 4059 ExecStart=/home/gal/my_services/zigbee2mqtt_service/z2m.sh (code
 Main PID: 4059 (code=exited, status=203/EXEC)

Jul 20 11:00:57 phoenix systemd[1]: zigbee2mqtt.service: Service hold-off time o
Jul 20 11:00:57 phoenix systemd[1]: zigbee2mqtt.service: Scheduled restart job, 
Jul 20 11:00:57 phoenix systemd[1]: Stopped zigbee2mqtt.
Jul 20 11:00:57 phoenix systemd[1]: zigbee2mqtt.service: Start request repeated 
Jul 20 11:00:57 phoenix systemd[1]: zigbee2mqtt.service: Failed with result 'exi
Jul 20 11:00:57 phoenix systemd[1]: Failed to start zigbee2mqtt.
lines 1-12/12 (END)

One Answer

Thanks to DrTron at this post, I've found the correct way to fix this issue which is to add the user to the group that has permissions to USB devices.

case someone else is interested, I’ve add my user ‘gal’ to the dialout group with

sudo adduser gal dialout

Then, the systemd service only needs to take care to zigbee2mqtt so I’ve changed the service to:

$ systemctl cat zigbee2mqtt.service 
# /etc/systemd/system/zigbee2mqtt.service
[Unit]
Description=zigbee2mqtt
After=network.target

[Service]
ExecStart=/usr/local/bin/npm start
WorkingDirectory=/opt/zigbee2mqtt
StandardOutput=inherit
StandardError=inherit
Restart=always
User=gal

[Install]
WantedBy=multi-user.target

Lastly, I’ve logged out and logged in and started the service with the new configuration.

$ systemctl daemon-reload
$ systemctl start zigbee2mqtt.service

Answered by Gal on November 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP