Order in which Linux services are stopped during reboot/shutdown

sizzzzlerz asked:

On a KVM VM running Centos 7.5, I have a service running that writes to an NFS-mounted file system. When a shutdown or reboot is issued, the service is throwing an exception that indicates that it does not have permission to write to a directory that is on this NFS file system. If, instead, I manually stop the service using systemctl, the service completes normally, i.e., no exception is thrown. Because of the exception, I am speculating that, before my service can write its final data to the NFS mount, the OS is shutting down the NFS service, thus removing the mounted volume. When the service tries to write to this location, it cannot because the directory is gone. Do NFS mounts get removed before user services are told to stop?

What is odd about this is that I have two versions of the VM. This one, that is has the issue, has an LUKS-encrypted root partition. The other VM has the same code but is running on an unencrypted partition. The OS is the same on both. I don’t see why this would stimulate this issue but it is the only difference in the VMs.

Is there some way I can ensure that my service is done before NFS is killed?

My answer:


Systemd both starts and stops services in parallel whenever possible. This is how it manages to do both so quickly compared to pre-systemd Linux distributions. When a service requires some other service or resource, this needs to be declared explicitly in that service’s unit file so that systemd can order them correctly. Without doing so, you can’t reliably predict whether the resource your service needs will be made unavailable while your service is still trying to use it.

In your particular case, you can use RequiresMountsFor= to specify that the unit needs a filesystem mounted at a given mount point. This goes in the [Unit] section.

[Unit]
RequiresMountsFor=/path/to/mountpoint

With this declared, your service will not start until the filesystem is mounted at that mountpoint, and will be stopped before the filesystem is unmounted.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.