Skip to main content
added 147 characters in body
Source Link
jaskij
  • 200
  • 1
  • 9

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.

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.

Source Link
jaskij
  • 200
  • 1
  • 9

How to stop a service when a network interface goes down?

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.

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