I would like to create a service file that, when started, causes a specified filesystem to be mounted before the action of the service file is performed (as it needs that file system to be available). Furthermore, I would like this service file to fail gracefully in case the file system cannot be mounted (e.g., because it is located on a sometimes unplugged external hard drive).

My first try was simple and partly successful: add x-systemd.automount as an option in the filesystem's fstab line. Then the filesystem is automatically mounted when the service file causes its mountpoint to be accessed. However, it does not fail gracefully in the sense that there is a long timeout and it is logged as an ERROR.

My second try was not as successful, I added BindsTo=path-to-mountpoint.mount and After=path-to-mountpoint.mount to the service file (x-systemd.automount was removed): the path-to-mountpoint.mount is not found. I guess it only is created upon successful mount, so it is not meant for what I want to do.


1 Answer 1


According to the manual:

Mounts listed in /etc/fstab will be converted into native units dynamically at boot and when the configuration of the system manager is reloaded.

This means that you can add the following to the [Unit] section of your .service file:


(this is and example for my case where I mount /opt separately in my fstab file).

  • 2
    I ran across this helpful answer but did not know the naming schema for the mounts. Here is a quick way to find them: systemctl list-units | grep -o '.*\.mount'
    – qq4
    Aug 14, 2022 at 2:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .