Sample Header Ad - 728x90

Watching for /sys/class/net file changes using systemd

0 votes
1 answer
400 views
I'm trying to get a systemd service to restart when some ethernet cable gets plugged in, by monitoring changes to the /sys/class/net/eth0/carrier_up_count system file. I tried creating a systemd path service:
[Path]
PathModified=/sys/class/net/eth0/carrier_up_count
Unit=restart-other-service.service
but, while I can see that the carrier_up_count indeed changes when I plug the cable in, this path service never gets triggered. I assume this happens because systemd path services actually monitors when files are open and closed for writing, and the /sys/ files are always open (I can see that the last modified timestamp of the file doesn't change even though the contents of the file does). I also tried using a systemd socket service with ListenSpecial=/sys/class/net/eth0/carrier_up_count, but the service failed:
Got unexpected poll event (0x9) on socket.
Failed with result 'resources'.
Is there a way to get systemd to monitor changes on the files in /sys/ ? I guess I could just create a cronjob that reads the target file periodically but it feels like there should be a better way.
Asked by Quentin (25 rep)
May 19, 2024, 08:19 PM
Last activity: May 19, 2024, 08:49 PM