I'm running Arch Linux, and I have a udev rule which starts a service when a device is inserted. In this case, it dials a connection when a 3G modem is plugged in.
KERNEL=="ttyUSB*", SYMLINK=="gsmmodem", TAG+="systemd", ENV{SYSTEMD_WANTS}="[email protected]"
However, if the device is removed, systemd won't stop the service, and hence when it is plugged in again, it won't start the service, since it's already running.
What I need is a matching udev rule which runs when the device is removed to stop the service.
Update
Using the answer below, what I now have is the following udev rule
KERNEL=="ttyUSB*", SYMLINK=="gsmmodem", TAG+="systemd", ENV{SYSTEMD_WANTS}="vodafone.service"
with the following service file (which was basically copied and pasted from the netcfg service file:
[Unit]
Description=Netcfg networking service for Vodafone Dongle
Before=network.target
Wants=network.target
BindsTo=dev-gsmmodem.device
After=dev-gsmmodem.device
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/netcfg check-iface wvdial
ExecStop=-/usr/bin/netcfg down wvdial
KillMode=none
[Install]
WantedBy=multi-user.target
I'm using netcfg-wvdial from the AUR to do the dialing.