1

I have a service which I want to start and stop my service when a specific network interface (eth0 in my case) goes up and down. Reading through documentation, and posts, I have come to the unit below, which, unfortunately, does not work.

To clarify: what I want is that when eth0 goes up my-service.service is started, and when eth0 goes down my-service.service is stopped.

The interface in question is managed by systemd-networkd, has a static IP and a DHCP server.

Is there an option to do this purely with systemd, or is my only option a different service, such as networkd-dispatcher?

The system is running a custom distribution made with Yocto Project, with systemd 250.4 and networks managed by systemd-networkd. But I welcome answers from any distro, as they might point me to an answer, or be helpful to others.

my-service.service

[Unit]
BindsTo=sys-subsystem-net-devices-eth0.device
After=sys-subsystem-net-devices-eth0.device
BindsTo=nats.service
After=nats.service
BindsTo=postgresql.service
After=postgresql.service

[Service]
Type=simple
Restart=on-failure
EnvironmentFile=/opt/my/environment.txt
ExecStart=/opt/my/executable

[Install]
WantedBy=multi-user.target

systemctl list-units doesn't show anything interesting:


root@device:~# systemctl list-units | grep -i net
  sys-devices-platform-bus\x405a000000-5a8e0000.can-net-can0.device                                           loaded active plugged   /sys/devices/platform/bus@5a000000/5a8e0000.can/net/can0
  sys-devices-platform-bus\x405a000000-5a8f0000.can-net-can1.device                                           loaded active plugged   /sys/devices/platform/bus@5a000000/5a8f0000.can/net/can1
  sys-devices-platform-bus\x405b000000-5b040000.ethernet-net-eth0.device                                      loaded active plugged   /sys/devices/platform/bus@5b000000/5b040000.ethernet/net/eth0
  sys-devices-platform-bus\x405b000000-5b050000.ethernet-net-eth1.device                                      loaded active plugged   /sys/devices/platform/bus@5b000000/5b050000.ethernet/net/eth1
  sys-subsystem-net-devices-can0.device                                                                       loaded active plugged   /sys/subsystem/net/devices/can0
  sys-subsystem-net-devices-can1.device                                                                       loaded active plugged   /sys/subsystem/net/devices/can1
  sys-subsystem-net-devices-eth0.device                                                                       loaded active plugged   /sys/subsystem/net/devices/eth0
  sys-subsystem-net-devices-eth1.device                                                                       loaded active plugged   /sys/subsystem/net/devices/eth1
  dhcpcd.service                                                                                              loaded active running   A minimalistic network configuration daemon with DHCPv4, rdisc and DHCPv6 support
  rdisc.service                                                                                               loaded active running   Network Router Discovery Daemon
  systemd-network-generator.service                                                                           loaded active exited    Generate network units from Kernel command line
  systemd-networkd.service                                                                                    loaded active running   Network Configuration
  systemd-resolved.service                                                                                    loaded active running   Network Name Resolution
  systemd-timesyncd.service                                                                                   loaded active running   Network Time Synchronization
  xinetd.service                                                                                              loaded active running   Xinetd A Powerful Replacement For Inetd
  systemd-networkd.socket                                                                                     loaded active running   Network Service Netlink Socket
  network-pre.target                                                                                          loaded active active    Preparation for Network
  network.target                                                                                              loaded active active    Network
  nss-lookup.target                                                                                           loaded active active    Host and Network Name Lookups
4
  • Check if this post is useful for your task: unix.stackexchange.com/questions/360214/…
    – thanasisp
    May 12, 2022 at 18:00
  • @thanasisp thanks, I think I have seen that earlier, but did go over it once again. netplugd seems unmaintained, and Yocto has a ready made recipe for networkd-dispatcher. The other answers there are of no use to me - BindsTo= probably works for wireless interfaces, but for my wired case it doesn't seem to. Another option which just came to mind is to have my daemon work all the time and just check ethernet state via DBus.
    – jaskij
    May 12, 2022 at 18:12
  • 1
    Probably, you could use ifplugd as the daemon to do the job and start/stop your service.
    – thanasisp
    May 12, 2022 at 21:30
  • @thanasisp holy hell, 17 years with no updates. Regardless, thanks, that's an alternative. I might not have been clear in my question - I'm not opposed to networkd-dispatcher specifically, but to simply pulling in another dependency. It's good to have alternatives, but that's about it.
    – jaskij
    May 12, 2022 at 22:17

1 Answer 1

-1

If I understand correctly, you want a custom notification that network is down?

Than just add a key "ExecStopPost" in your my-service.service, which would point to a script/application you need to run on service down.

Read here for full explanation of .service file: https://www.freedesktop.org/software/systemd/man/systemd.service.html

6
  • No, you didn't understand correctly. I want it to go as follows: eth0 goes up, service starts. eth0 goes down, service stops.
    – jaskij
    May 11, 2022 at 15:37
  • Then just call your script in ExecStart/ExecStop of eth0 service description. And call ifup/ifdown from your script.
    – White Owl
    May 11, 2022 at 15:43
  • There's no eth0 service, at least none that I control - it is a network interface, not a service.
    – jaskij
    May 11, 2022 at 15:45
  • Also, I have a service, not a script to call
    – jaskij
    May 11, 2022 at 15:46
  • If you do not have eth0 service - you can create it. Modern systems usually have just a network.service which call ifup -a. In you case you would have multiple .service files with a ifup eth0, ifup wlan0, etc. One service file per interface. And even though .service file is not a script itself, you call scripts from it, and call it from scripts. Here is a good tutorial: digitalocean.com/community/tutorials/…
    – White Owl
    May 11, 2022 at 16:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .