Wait for Network Ping for fstab mount

There are cases where it is absolute requirement to have working networking to be able to mount a remote folder. CIFS mounts are examples of that, even more so when a virtual bridge is involved since they appear to take longer to be available and working (either that or systemd services may not be properly waiting for it to be ready before continuing).

The steps below will create a new systemd service that can be set as required service to be active (here we will ping a certain local IP) before attempting to mount that folder.

  • Create the /etc/systemd/system/wait-for-ping.service file with the following contents. The can be replaced with any IP address, even public ones if you need to wait for the internet to be reachable.

    Description=Wait for a successful ping
    ExecStartPre=/usr/bin/bash -c "while ! ping -c1; do sleep 1; done"
    ExecStart=/usr/bin/bash -c "Ping successful. Network is reachable."

  • Reload systemctl config files

    sudo systemctl daemon-reload

  • Enable and start (with the --now flag) our new service.

    sudo systemctl enable --now wait-for-ping.service

  • Edit /etc/fstab and add x-systemd.after=wait-for-ping.service in the mount options to entries of your choosing. For example, for a CIFS mount this would look like this:

    //    /mnt/foobar       cifs    x-systemd.after=wait-for-ping.service,uid=1001,gid=1001,dir_mode=0775,file_mode=0664,credentials=/root/creds,iocharset=utf8 0 0