[systemd-devel] Ensuring that a unit starts before any networking

Ede Wolf listac at nebelschwaden.de
Sat Jun 27 11:54:44 UTC 2020


Just a wild guess, but I'd start with a combination of one of those:

# -----


[Unit]
Description=my service

Before=network.target
Before=systemd-networkd.service
Before=network-online.target
Before=systemd-networkd-wait-online.service

...

[Install]
WantedBy=Basic.target

# -----


Probably someone will kill me for the basic target, which is fine, as 
long as the proper one is provided as well, and preferably with an 
explanation :)

In fact, probably I'd create a new, dedicated target for that, like 
my.target.

And if your script HAS to be finished, for networking to be run at all, 
you might want to add a drop in to systemd-networkd.service

# -----

systemctl edit systemd-networkd.service

[Unit]
Requires=my.service
After=my.service

# -----

This is most likely not a final solution, but a way I would approach 
this matter and start to play.
F.e., I am not sure, wether the drop in should really be for 
systemd-networkd.service or rather network.target. Play around.

And I am assuming, you are using systemd-networkd, and not something 
else  like networkmanager, in that case you have to adapt, of course.

Until someone with proper knowledge replies, good luck and report back



Am 27.06.20 um 10:34 schrieb Mark Rogers:
> This feels like something I should be easily able to answer from
> documentation/Google, and failing that from somewhere like
> StackOverflow, without troubling systemd-devel, but all my efforts
> have thus far failed [1]
> 
> What is the correct way to ensure a script runs to completion before
> any networking units start? "The Internet" is abound with people
> asking the opposite question which hasn't helped my keyword-based
> searches.
> 
> My use case (on a Raspberry Pi running Raspbian if it matters) is that
> I have network settings stored in a database from which I generate
> network configuration files (eg dhcpcd.conf) on boot, therefore they
> obviously need to be in place before dhcpcd starts. (Similarly
> wpa_supplicant.)
> 
> Ideally I'd like to be agnostic about the actual network stack in my
> unit configuration (so not setting dependencies relating to dhcpcd,
> for example) but at this point I've tried pretty much everything
> without success so I'm obviously doing something stupid.
> 
> I have tried multiple approaches so far but by current service file
> looks like this:
> 
> [Unit]
> Description=Config generation from DB
> Before=networking.service
> 
> [Service]
> Type=oneshot
> ExecStart=/home/mark/bin/db2config.py
> 
> [Install]
> RequiredBy=network.target
> 
> [1] https://stackoverflow.com/questions/62574482/ensuring-that-a-systemd-unit-starts-before-any-networking
> - no responses there to date, feel free to respond there for
> reputation or else I'll update it when I solve it.
> 



More information about the systemd-devel mailing list