Systemd wait for network interface to be up before running service
Solution 1
I solved this problem by looking at the output of:
systemctl list-units --no-pager
That showed me many units that I didn't expect like all the network devices!
sys-devices-virtual-net-lan0.device loaded active plugged /sys/devices/virtual/net/lan
So I added
BindsTo=sys-devices-virtual-net-lan0.device
After=sys-devices-virtual-net-lan0.device
to my unit service file and then my service didn't start until lan0 was available.
Solution 2
This seems to be incorrect. The correct name is network-online.target
. network-online.service
does not exist (tested on Fedora 26).
https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/
Related videos on Youtube
nasataxidermprefix
Just a guy trying to get through life one line of code a day...
Updated on September 18, 2022Comments
-
nasataxidermprefix over 1 year
I have a couple questions about systemd. I'm having issues consistently getting my script to run once the network interface is up. I have tried Requires and After as seen below but is inconsistent with waiting for the network to be up. Am I using the right service and implementing it correctly? To by pass this right now I am running a ping check loop which is very inefficient and hackish. Any advice would be great. Thanks!
[Unit] Description=PBU installer Requires=network-online.service After=network-online.service [Service] Type=oneshot ExecStart=/home/pbu/current/scripts/pbu-unpack.sh RemainAfterExit=yes [Install] WantedBy=multi-user.target
-
rogerdpack almost 5 years
-
-
nasataxidermprefix over 8 yearsI have tried it... I'll try it again. Would i insert it as a parameter for Requires and After?
-
smokes2345 over 8 yearsWhen using "Requires", all the listed service units will start together. When using "After", systemd will make sure that all other listed units will start before yours, but will not necessarily start yours with them. One or the other should be sufficient, seems to me that both would cause a conflict.
-
nasataxidermprefix over 8 yearsI think the problem is when I use requires OR after, the service starts with or after, but I almost need the network service to finish or at least get to a certain point... or else my script will require the network before it is actually up...
-
nasataxidermprefix over 8 yearsUnfortunately systemd doesn't have this option. WIsh it did, would save me a lot of trouble.
-
smokes2345 over 8 yearsThat's why I suggested using network.target. Targets indicate a particular milestone in the boot process or a group of units that work together, instead of an individual service. The idea behind using "After=network.target" is that all the units that are needed to provide network access should be activated before your unit is activated.
-
0xC0000022L almost 4 yearsThere's another catch with this method. What does it mean to be online? Just because a particular network interface is up doesn't mean that the service that I want to tie to a totally different interface can start ...