Running as systemd service

I’m running zigbee2mqtt on CC2531 (with antenna, forgot its model) and a Linux machine.
Sometimes zigbee2mqtt stops working and I have to change the ownership of the device (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/systemctl for 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, it won’t run now. I’m tried enabling, starting manually, and 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’.

The devices in /dev belong to root and should remain so.
But you can easily solve that problem by adding your user “gal” to the “dialout” group (which is the group the ttyACM-devices belong to). As the group also has rw-rights, that will give the user access to the device.

1 Like

Thanks!
That’s the correct way and it worked!

In 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
1 Like