Home Make systemd service wait for mount
Post
Cancel

Make systemd service wait for mount

Services in systemd can be set to wait on the availability of any specific mount point in the local filesystem. This is very useful for NFS mounts as those require the network to be up and running, which can take a while.

This feature is controlled through the RequiresMountsFor option in the unit file of the service.

For instance, I use RequiresMountsFor in the fail2ban service that is running alongside the reverse proxy of a NextCloud instance. Hence, I can give access to fail2ban to the logs from the remote NextCloud and block connections in the reverse proxy not only based on its own activity, but also based on the activity in NextCloud itself.

The following steps show how to modify the existing service for fail2ban to wait for the folder with remote log files that is mounted with NFS:

  1. Edit the unit file

    1
    
     $ sudo systemctl edit fail2ban.service
    
  2. Set RequiresMountsFor in the Unit section to the absolute path needed by the service

    1
    2
    
     [Unit]
     RequiresMountsFor=/var/log/nextcloud
    

systemd will mount all mounts needed to access the path in RequiresMountsFor, even those with noauto.

This post is licensed under CC BY 4.0 by the author.