当网络接口出现故障时如何停止服务?

当网络接口出现故障时如何停止服务?

我有一项服务,我想在以下情况下启动和停止我的服务:特定网络接口eth0就我而言)会上升和下降。通过阅读文档和帖子,我来到了下面的单元,不幸的是,它不起作用。

澄清一下:我想要的是当eth0上升时my-service.service开始,当eth0下降时my-service.service停止。

有问题的接口由 管理systemd-networkd,具有静态 IP 和 DHCP 服务器。

是否有一个选项可以纯粹使用 systemd 来执行此操作,或者我唯一的选择是不同的服务,例如networkd-dispatcher

该系统正在运行由 Yocto Project 制作的自定义发行版,具有systemd250.4 和由systemd-networkd.但我欢迎任何发行版的答案,因为它们可能会向我指出答案,或者对其他人有帮助。

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没有显示任何有趣的东西:


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

答案1

如果我理解正确的话,您想要一个网络已关闭的自定义通知吗?

不仅仅是在 my-service.service 中添加一个键“ExecStopPost”,它将指向您需要在服务关闭时运行的脚本/应用程序。

请阅读此处了解 .service 文件的完整说明: https://www.freedesktop.org/software/systemd/man/systemd.service.html

相关内容