Start a Service After a Resource is Mounted
If you have network resources, such as NFS or iSCSI-based mounts, and you want to make sure that another service only starts after the resource is mounted, then consider the following example setup when configuring your system.
The example below is based on an NFS mount which you want to be available before the service with <name.service> starts. The same procedure can be used for iSCSI. For details setting up an iSCSI mount see the Ubuntu iSCSI Initiator guide.
In general, the name in <name.service> could be any valid service like
docker or others.
_netdevto the list of NFS mount point options in
This option ensures that the mount happens after the network is up:
resource:foreign_path local_path nfs (<your options>),_netdev
Make sure that all mounts in
/etc/fstabare mounted by running:
sudo mount -a
Run the following command to list mounts which must be up first:
systemctl list-units | grep -nP "\.mount"
You should see lines printed to the console. Look for the mount you want to be up.
<folder.mount> loaded active mounted <local_path>
Edit the service you want to change:
sudo systemctl edit <name>.service
An editor opens. Add the following directive, using your chosen
If needed, you can add more than one dependency by separating them with spaces. This procedure keeps
<name>.servicein its original state but makes it possible to override the current setup with new parameters. This is necessary because during updates the original service data will be overwritten. It automatically creates a directory in
<name>.service.dand a file in that directory called
override.conf. In the example above, the parameter is added to the existing list of parameters of the
For more details read section Example 2. Overriding vendor settings.
Keep the following points in mind with regard to whether
<name>.serviceis linked or not:
If the file is linked from
/lib/systemd/system, it is for packaged unit files. They are overwritten when Systemd (or whatever package provides them) is upgraded.
If the file originates in
/etc/systemd/system, it is for your own, possibly customized unit files. Unit files you place in here override the package-provided files and will not be replaced during an upgrade.
It is recommended to keep things simple and future-proof by creating an override file via
Run the following command to apply your changes:
sudo systemctl daemon-reload
<name>.servicehas been properly added:
sudo systemctl show <name>.service | grep "After="
folder.mountshould be part of the parameter list.
Restart your service by invoking:
sudo systemctl restart <name>