How to write a Systemd unit that will fire before networking goes down

systemd

I want to run a script when a user shuts their computer down, but it needs networking. So I'd like it to run when the system is heading for shutdown (or restart), but it needs to run before anything is unmounted or networking is shut off.

How can I do this?

Best Answer

This question is very similiar to How to run a script with systemd right before shutdown?. With the twist that you want to be sure networking is up.

To cover that, add this to your service file:

After=networking.service

As the docs say, After= not only declares that your service is started by the networking service, it also is declaring that the services should be stopped in the inverse order-- before networking is shut down.

Related Question