I want my service to only start when network is completely configured (ip address, interfaces, etc.), and restart my service whenever network restarts(because configuration may change). To achieve this I thought I'll need to add After=network.target and Wants=network.target to my service file. So that whenever network is restarted my service also restarts and only starts after network is completely up.
In my centos 7 VM when I restart network with command /etc/init.d/network restart I notice that there's no change in network.target, it's still up and not restarting, I tried installing NetworkManager and restart with it, but I didn't see any change. I noticed network.service restarted. Should we be using network.service instead of network.target? In all the online resources it is given how to use network.target or network-pre.target, network-online.target, etc.
In my Ubuntu 18.04 VM, I tried restarting network with command /etc/init.d/networking restart, but it says file itself is not present, maybe something is wrong with my VM? Here also I tried by installing Network-Manager, but there seems to be no change in network.target status. Here I also noticed that there's no network.service file. Here we are using neplan config file, but when I restart using network-manager, there's no change.
How to write my service file? Or service file is correct but I'm not properly restarting network? Should service file be different for rpm based distros and deb?
PartOf=network.targetorPartOf=network.service(I think it should be the second one)network.serviceis an alias toNetworkManager.serviceso maybe you want to useNetworkManagerinstead.network.targetshould be refused so you will not be able to (re)start manually (but if you stop manually and your custom systemd service has:PartOf=network.targetthen it should stop the unit). However I guess you should use:network-online.targetinstead ofnetwork.target. From this page: "network-online.target is a target that actively waits until the nework is "up", where the definition of "up" is defined by the network management software."network-online.targetand if you do, then usePartOf=network-online.servicein your service. Btw, aboutCentosare you usingsystemd? Because I see you are using/etc/init.d/network restartwhich is part of theinitdaemon but maybe that's irrelevant because some systems redirectinitscript to systemd scripts (AFAIK, I noticed this behavior inParrot Os)