[arch-general] Automatically stop systemd service
TL;DR: More toying with mobile broadband and systemd; would like help automatically stopping service depending on device availability. I’ve been adjusting the .service file for netcfg@ppp-mobile.service so it plays nicely with my modem. It will only start if the USB modem is plugged in and exposing a serial port to the computer, and when I manaually stop the unit, the ppp daemon is also killed, so it doesn’t lock the device and reconnect automatically upon the device being detected again, thus *bringing up the network interface when systemd and netcfg say it is down*. /etc/systemd/system/netcfg@ppp-mobile.service: ---------------------------------------------- .include /usr/lib/systemd/system/netcfg@.service [Unit] Description=Netcfg networking service for mobile broadband point to-point protocol. ConditionPathExists=/dev/ttyUSB0 [Service] KillMode=control-group What I cannot figure out how to do is automatically stop the service if the device is unplugged, similar to how ifplugd works for wired ethernet interfaces. I’ve read the manual pages and asked Google, and I cannot seem to find more detailed information. I think it may be possible to write a service that will use udev’s polling capabilities, but then I have to somehow link that unit to the netcfg one such that it gets stopped and started when netcfg is, which implies it would listen to udev, wait for the device to be unplugged, then stop netcfg and kill itself. systemd's PartOf= may offer an answer, but I don’t really know how to tie it all together. How should I best go about this? Another systemd service, a udev rule, or is it possible to further modify the ppp-mobile service? regards, Robbie
On Thu, 28 Mar 2013 18:23:03 +1100 Robbie Smith <zoqaeski@gmail.com> wrote:
TL;DR: More toying with mobile broadband and systemd; would like help automatically stopping service depending on device availability.
[...]
What I cannot figure out how to do is automatically stop the service if the device is unplugged, similar to how ifplugd works for wired ethernet interfaces.
How about "BindsTo=" from man 5 systemd.unit? For example, "BindsTo=sys-subsystem-net-devices-wap.device". -- Leonid Isaev GnuPG key: 0x164B5A6D Fingerprint: C0DF 20D0 C075 C3F1 E1BE 775A A7AE F6CB 164B 5A6D
On 29/03/13 00:39, Leonid Isaev wrote:
On Thu, 28 Mar 2013 18:23:03 +1100 Robbie Smith <zoqaeski@gmail.com> wrote:
TL;DR: More toying with mobile broadband and systemd; would like help automatically stopping service depending on device availability.
[...]
What I cannot figure out how to do is automatically stop the service if the device is unplugged, similar to how ifplugd works for wired ethernet interfaces.
How about "BindsTo=" from man 5 systemd.unit? For example, "BindsTo=sys-subsystem-net-devices-wap.device".
That looks like the right starting point. “PartOf=” seems like it’s intended to control units, not devices. There’s a slight problem though: not only are the device names quite verbose[*], they aren’t persistent. Only the lowest-numbered USB serial port will communicate with the device; usually this is /dev/ttyUSB0 but sometimes it’s not. I have no idea what the other two or three do, and I couldn’t find documentation on minicom (or similar) to be able to probe them. The new Linux persistent naming rules don’t apply to these devices, and udev rules seem so arcane I could never figure out how to write a rule that would give a consistent name to the accessible port. [*] There do appear to be aliases to their names in /dev/, so this probably isn’t an issue.
On 29/03/13 00:39, Leonid Isaev wrote:
On Thu, 28 Mar 2013 18:23:03 +1100 Robbie Smith <zoqaeski@gmail.com> wrote:
TL;DR: More toying with mobile broadband and systemd; would like help automatically stopping service depending on device availability.
[...]
What I cannot figure out how to do is automatically stop the service if the device is unplugged, similar to how ifplugd works for wired ethernet interfaces.
How about "BindsTo=" from man 5 systemd.unit? For example, "BindsTo=sys-subsystem-net-devices-wap.device".
That did the trick (when the device is /dev/ttyUSB0). Now to just make a persistent name for the rare occasions when it appears as something else (always a /dev/ttyUSBx).
On Fri, 29 Mar 2013 01:48:55 +1100 Robbie Smith <zoqaeski@gmail.com> wrote:
On 29/03/13 00:39, Leonid Isaev wrote:
On Thu, 28 Mar 2013 18:23:03 +1100 Robbie Smith <zoqaeski@gmail.com> wrote:
TL;DR: More toying with mobile broadband and systemd; would like help automatically stopping service depending on device availability.
[...]
What I cannot figure out how to do is automatically stop the service if the device is unplugged, similar to how ifplugd works for wired ethernet interfaces.
How about "BindsTo=" from man 5 systemd.unit? For example, "BindsTo=sys-subsystem-net-devices-wap.device".
That did the trick (when the device is /dev/ttyUSB0). Now to just make a persistent name for the rare occasions when it appears as something else (always a /dev/ttyUSBx).
Glad to help :) Regarding the persistend device names, you'll have to place a udev rule in /etc/udev/rules.d/, e.g. (there is also an archwiki page on this) --- /etc/udev/rules.d/20-nic.rules SUBSYSTEM=="net", ATTR{address}=="<MAC>", NAME="wan" --- Although this is for a network card (I have never seen a 3g/4g modem). You'll have to run something like "udevadm info --path=/sys/class/net/wan --attribute-walk" replacing 'wan' with your device name to find persistent attributes. L. -- Leonid Isaev GnuPG key: 0x164B5A6D Fingerprint: C0DF 20D0 C075 C3F1 E1BE 775A A7AE F6CB 164B 5A6D
participants (2)
-
Leonid Isaev
-
Robbie Smith