Linux: Start daemon on connected USB-serial dongle

Since the link in my further comment seems to solve this problem, here is the solution for using udev for starting a daemon when a certain usb device is plugged in:

Proper(-ish) way to start long-running systemd service on udev event (device hotplug)


Create a udev rule like

# cat /etc/udev/rules.d/95-serialdaemon.rules
KERNEL=="ttyUSB0", TAG+="systemd", ENV{SYSTEMD_WANTS}="serialdaemon.service"

Create a systemd service like

# cat /lib/systemd/system/serialdaemon.service
[Unit]
Description=USB serial to socket bridge
After=remote-fs.target
After=syslog.target

[Service]
ExecStart=/mnt/serialdaemon.sh

Create the executable file

# cat /mnt/serialdaemon.sh
#!/bin/sh
date +%F-%T >> /var/log/serialdaemon.log
/usr/local/serialdaemon/serialdaemon -serial /dev/ttyUSB0 -port 15789 -baud 38400 >> /var/log/serialdaemon.log 2>&1
date +%F-%T >> /var/log/serialdaemon.log