Set systemd service to execute after fstab mount

a CIFS network location is mounted via /etc/fstab to /mnt/ on boot-up.

No, it is not. Get this right, and the rest falls into place naturally.

The mount is handled by a (generated) systemd mount unit that will be named something like mnt-wibble.mount. You can see its actual name in the output of systemctl list-units --type=mount command. You can look at it in detail just like any other unit with systemctl status.

Very simply, then: you have to order your unit to be started after that mount unit is started.

After=network.target vpn-launch.service mnt-wibble.mount

Further reading

  • https://unix.stackexchange.com/a/236968/5132

Although both answers are correct, I want to add my two cents to the discussion, because when I have looked for it I was missing some instructions and examples of how to proceed.

  1. Add the filesystem to /etc/fstab
  2. Type mount -a which mounts all filesystems mentioned in fstab
  3. Look for the systemd unit that has been generated with:

    systemctl list-units | grep '/path/to/mount' | awk '{ print $1 }'

    (should return something that ends with .mount)
  4. Add the found mount-unit to the After= statement in the *.service file

Here is an example of starting the my-daemon service on boot but after the network is ready, a CIFS share is mounted at /mnt/cifs, and the vpn-launch service has started:

/etc/fstab

//servername/sharename /mnt/cifs cifs defaults,some,other,options 0 0

Note: You may want to add nofail to your fstab options (e.g. when using an external drive). Otherwise, your machine won't boot if the device is not connected. See ArchWiki's fstab article


/etc/systemd/system/my-daemon.service

[Unit]
Description=Launch My Daemon
Requires=vpn-launch.service mnt-cifs.mount
After=network.target vpn-launch.service mnt-cifs.mount

[Service]
ExecStart=/path/to/my-daemon

[Install]
WantedBy=multi-user.target

Do not forget to enable the service such that it is started on boot: systemctl enable my-daemon

Note that this works for other filesystems (NFS, HDDs, etc.) as well.

As already mentioned, both answers are correct and I encourage everybody to read them but for me a couple of examples would have saved me some time.

Update (2019-06-25):

  • added a note regarding fstab options to prevent boot lock when using external drives
  • added mnt-cifs.mount to the Requires= list which causes the my-daemon.service fail to start-up when the cifs mount was not successfully mounted

Sorry but I can't comment yet.

Like JdeBP said, you should be ordering on the filesystem mount. You can predict the name of the mount unit or, alternatively, you can use (in unit section):

RequiresMountsFor=/absolute/path/of/mount

This option creates the dependencies to the appropriate *.mount units to make the path accessible before starting the service. It may not be on all systemd versions, but I've been using it in a CentOS 7 machine for the last 6 months or so.

Tags:

Debian

Systemd