I would like to run a systemd service each each time eth0 goes up/down (perhaps a separate service for up and down). This value should change when the ethernet is plugged in. Does anybody know how to accomplish this?

I could write a bash script to poll every X number of seconds, but that may eat up more CPU than I would like and may not be as clean of an implementation.

Any suggestions are welcome. Thanks!


Ethernet up/down events (and much more) are reported by the kernel over a netlink socket. You watch the messages with ip monitor, which could presumably be used in your shell script to avoid polling.

I also found that Debian at least has a package for netplug, which is a daemon that listens for netlink messages, and then runs a shell script on link add, up, and down. Your shell script can then use systemctl to start/stop your units.

(Presumably, Network Manager and systemd-networkd also listen for these events, and there is probably a way to hook a script off them—but you don't seem to be using them.)

  • Thanks for the answer. I may like to use the systemd-networkd if I can avoid writing a shell script that is a daemon that way. What do you think? – ChrisVollo Apr 20 '17 at 17:37
  • @ChrisVollo if you're running systemd on a system, it's a reasonable way to configure networking (provided you don't need something more complicated, like Network Manager). I'm not sure how easy it is to hook arbitrary shell scripts (or other units) in to, though. It's a bit hard to provide advice though, as you haven't explained what you're trying to accomplish (what is the ultimate goal). – derobert Apr 20 '17 at 17:39
  • thanks! Yes, basically what I am looking to do is to find a good way to run a certain set of commands when the ethernet cable is plugged in, and a certain other set of commands when the ethernet cable is unplugged. – ChrisVollo Apr 20 '17 at 17:43
  • @ChrisVollo I got that part—but I have no idea why you want to run those commands. E.g., if it's to configure the interface, systemd-networkd can probably already do that without running your script. – derobert Apr 20 '17 at 17:54

This question on ServerFault indicates that the solution would be as simple as adding one line to the [Install] section of your custom service:

WantedBy=<the appropriate device unit>.device

If, on the other hand, you want your service to start when your system's network connection is online, no matter what type the network connection might be, then see man systemd.special and read the instructions for the use of network-online.target specific to your version of systemd. For the version of systemd on my system, the recommended procedure is to add this to your service:


... but I think I've seen different instructions for different versions of systemd here, so check your distribution's man systemd.special first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.