ネットワークインターフェイスが失敗したときにサービスを停止する方法は?

ネットワークインターフェイスが失敗したときにサービスを停止する方法は?

サービスがありますが、次の場合はサービスを開始して停止したいと思います。特定のネットワークインターフェース上下に移動します(eth0私の場合)。ドキュメントと投稿を読み、以下のデバイスにアクセスしましたが、残念ながら動作しません。

はっきりとしたら、私が望むのはeth0上がるときにmy-service.service始まり、eth0下がるときに止まることですmy-service.service

問題のインターフェイスは管理systemd-networkdされ、静的IPとDHCPサーバーがあります。

systemdを使用してこれを行うオプションはありますか?または唯一のオプションは、次のような他のサービスです。networkd-dispatcher

システムはYoctoプロジェクトで作成された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

おすすめ記事