Retry Failed NFS and CIFS Mounts with a systemd Timer

Recover NFS and CIFS mounts after boot with three timed retries, clear journald errors, and troubleshooting commands for Debian, Ubuntu, and Proxmox.

On this page

I had a little annoying issue. Just updated my Proxmox host and rebooted it. Later, my Home Assistant backup sync to NAS failed because its NFS destination wasn’t mounted.

The NAS was on. The host had come back up. But three NFS mounts had timed out during boot and stayed down. But, when i mount them manually, it worked.

The boot log showed a brief network link interruption while the mounts were starting. That gave me a hint, though it didn’t explain the whole timeout. I wanted the host to try again once things had settled, then leave a clear failure in the journal if it still couldn’t mount the shares.

This is the recovery pattern I used, adapted here for either NFS or CIFS/SMB shares. It works with normal /etc/fstab mounts on systemd-based Debian, Ubuntu, and Proxmox hosts.

TL;DR

  • Keep your working mount definitions in /etc/fstab.
  • Check them 10 minutes after boot, with up to three recovery attempts and 10-minute gaps between attempts.
  • Stop when the shares are working. After the third failure, log MOUNT_RECOVERY_FAILED and leave a failed systemd service to investigate.

This handles mounts that fail during startup. Once recovery succeeds, the timer stops for that boot. It won’t keep watching for a NAS outage later in the day.

1. Check the mount before adding retries

Run these instructions as root in Bash. On Debian or Ubuntu, start with sudo -i. The Proxmox shell normally already runs as root.

Install the client helper for the share you use:

Terminal window
# NFS
apt install nfs-common
# CIFS / SMB
apt install cifs-utils

The script also uses findmnt, mountpoint, flock, and timeout, normally available through the standard util-linux and coreutils packages.

I’ll use these example mounts. Replace the addresses, shares, and local paths with yours. Keep only the entries you need.

/etc/fstab — example entries
192.0.2.10:/exports/backups /mnt/nas-backups nfs _netdev,nofail,vers=4.1 0 0
//192.0.2.20/team /mnt/team-share cifs _netdev,nofail,credentials=/root/.smb-team,vers=3.0 0 0

These addresses are placeholders. The CIFS example assumes /root/.smb-team already contains working credentials and is readable only by root. Keep your existing, tested options rather than replacing them just to match mine.

After editing fstab, create the mount directories and reload systemd’s generated units:

Terminal window
mkdir -p /mnt/nas-backups /mnt/team-share
systemctl daemon-reload

Check one share manually before installing recovery:

Terminal window
mount -v /mnt/nas-backups
findmnt --mountpoint /mnt/nas-backups

For CIFS, use /mnt/team-share instead. A successful findmnt result should show the remote share and its filesystem type. A plain local directory at that path isn’t enough.

If mounting fails every time, fix that error first. Retries won’t correct an NFS export permission or an expired SMB password.

The _netdev option identifies a network mount. nofail lets boot continue if it is unavailable, so programs using the path must still check it before writing. See the systemd mount options (opens in a new tab).

For NFS, I haven’t added soft just to make failures return faster. That changes how filesystem I/O fails and can risk data corruption. The NFS manual explains the tradeoff (opens in a new tab). The timeout below limits the recovery command, not normal application I/O.

2. Create the recovery script

Create /usr/local/sbin/mount-recovery with the complete script below. Edit the two rows under SHARES to match your fstab entries exactly. Remove a row if you only use one share. Use simple mount paths without spaces for this example.

Each row is a local mount path followed by its expected remote source. Checking the source helps avoid treating the wrong mounted filesystem as a successful recovery.

/usr/local/sbin/mount-recovery
#!/usr/bin/env bash
set -euo pipefail
state=/run/mount-recovery
mkdir -p "$state"
exec 9>"$state/lock"
flock -n 9 || exit 0
attempt=0
if [[ -f "$state/attempt" ]]; then
read -r attempt < "$state/attempt"
fi
if [[ ! "$attempt" =~ ^[0-3]$ ]]; then
systemctl stop mount-recovery.timer
echo '<3>MOUNT_RECOVERY_FAILED: invalid attempt counter'
exit 1
fi
if (( attempt >= 3 )); then
systemctl stop mount-recovery.timer
echo '<3>MOUNT_RECOVERY_FAILED: retries exhausted, reset before trying again'
exit 1
fi
attempt=$((attempt + 1))
printf '%s\n' "$attempt" > "$state/attempt"
echo "<6>MOUNT_RECOVERY_ATTEMPT: $attempt/3"
failed=()
while read -r path expected; do
if ! mountpoint -q "$path"; then
echo "<6>Mounting $path from fstab"
if ! timeout --kill-after=5s 60s mount "$path"; then
echo "<4>Mount command failed or timed out: $path"
fi
fi
actual=$(findmnt -rn -M "$path" -t nfs,nfs4,cifs -o SOURCE || true)
if [[ "$actual" != "$expected" ]]; then
echo "<4>Missing mount or unexpected source: $path"
failed+=("$path")
elif ! timeout --kill-after=5s 10s ls -U -- "$path" >/dev/null; then
echo "<4>Mounted but directory check failed: $path"
failed+=("$path")
else
echo "<6>Healthy: $path ($actual)"
fi
done <<'SHARES'
/mnt/nas-backups 192.0.2.10:/exports/backups
/mnt/team-share //192.0.2.20/team
SHARES
if (( ${#failed[@]} == 0 )); then
systemctl stop mount-recovery.timer
echo '<6>MOUNT_RECOVERY_OK: all shares healthy, stopping timer'
exit 0
fi
if (( attempt < 3 )); then
echo "<4>MOUNT_RECOVERY_RETRY: ${failed[*]}, next attempt in 10 minutes"
exit 0
fi
systemctl stop mount-recovery.timer
echo "<3>MOUNT_RECOVERY_FAILED: after 3 attempts: ${failed[*]}"
exit 1

Make it executable and check its syntax:

Terminal window
chmod 0755 /usr/local/sbin/mount-recovery
bash -n /usr/local/sbin/mount-recovery

I don’t want a recovery script force-unmounting a share while another job might be using it.

The directory check doesn’t provide any proof if the write access enabled or backup process was completed.

Kernel I/O stuck in an uninterruptible wait can also outlast a timeout signal.

3. Add the service and timer

Create /etc/systemd/system/mount-recovery.service. It runs the script once as root each time the timer activates it.

/etc/systemd/system/mount-recovery.service
[Unit]
Description=Recover failed NFS and CIFS mounts after boot
Wants=network-online.target
After=network-online.target remote-fs.target
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/mount-recovery
RuntimeDirectory=mount-recovery
RuntimeDirectoryMode=0700
RuntimeDirectoryPreserve=yes
TimeoutStartSec=5min
StandardOutput=journal
StandardError=journal
SyslogIdentifier=mount-recovery
SyslogLevelPrefix=yes
UMask=0077

RuntimeDirectoryPreserve=yes keeps the counter between attempts. It lives under /run, so it resets at reboot. The <6>, <4>, and <3> prefixes become info, warning, and error priorities in the journal. The prefixes themselves are removed. Both settings are covered in systemd’s execution options (opens in a new tab).

Five minutes gives these two shares room for their mount attempts and directory checks. Increase TimeoutStartSec if you add several more shares. Otherwise systemd could stop the script before it reaches its final failure message.

Now create /etc/systemd/system/mount-recovery.timer:

/etc/systemd/system/mount-recovery.timer
[Unit]
Description=Retry failed network mounts at ten minute intervals
[Timer]
OnBootSec=10min
OnUnitInactiveSec=10min
AccuracySec=1s
Unit=mount-recovery.service
[Install]
WantedBy=timers.target

The first check is due 10 minutes after boot. Further attempts run 10 minutes after the previous run finishes, so slow mount commands extend the schedule. The normal boot mount attempt is separate from these three retries. The systemd timer reference (opens in a new tab) explains those starting points.

Earlier failures return exit code 0 so the timer can continue. The third unsuccessful attempt returns 1, which leaves the service failed. A successful run stops the timer immediately.

4. Enable it and inspect the first run

Check the files, reload systemd, then enable the timer:

Terminal window
systemd-analyze verify /etc/systemd/system/mount-recovery.service \
/etc/systemd/system/mount-recovery.timer
systemctl daemon-reload
systemctl enable --now mount-recovery.timer
systemctl list-timers --all mount-recovery.timer

If the machine has already been up for more than 10 minutes, the first run is due immediately. Otherwise you can wait, or run one check now:

Terminal window
systemctl start mount-recovery.service
journalctl -b -u mount-recovery.service --no-pager

A manual run uses one attempt too. After success, the timer is enabled but inactive. That’s expected: it stopped for this boot and will start again at the next boot.

On my Proxmox host, the live check found all three NFS shares healthy and stopped its timer. I also checked the retry limit with simulated failures. The mixed NFS/CIFS example here uses generic paths, and I haven’t fully verified its CIFS behavior against a real SMB server or tested a reboot with it.

5. Find the mount failure in journald

Start with the current boot’s recovery log:

Terminal window
journalctl -b -u mount-recovery.service --no-pager

For warnings and errors, then only errors and higher priorities:

Terminal window
journalctl -b -u mount-recovery.service -p warning --no-pager
journalctl -b -u mount-recovery.service -p err --no-pager

To find the final message directly:

Terminal window
journalctl -b -u mount-recovery.service \
--grep='MOUNT_RECOVERY_FAILED' --no-pager
systemctl --failed

An example final log entry looks like this sample below:

Nov 26 10:35:00 server mount-recovery[1234]: MOUNT_RECOVERY_FAILED: after 3 attempts: /mnt/nas-backups

Remove the -p filter when investigating the cause. The earlier mount helper error is usually more useful than the final summary. You can also watch the next attempt live with:

Terminal window
journalctl -f -u mount-recovery.service

For the previous boot, use journalctl -b -1 -u mount-recovery.service. That needs retained logs from that boot. You can find more filters in the journalctl reference (opens in a new tab).

If the error still isn’t clear, inspect the original mount unit and kernel messages:

Terminal window
mount_unit=$(systemd-escape --path --suffix=mount /mnt/nas-backups)
journalctl -b -u "$mount_unit" --no-pager
journalctl -k -b --no-pager | grep -Ei 'nfs|cifs|smb|link.*(up|down)'

Replace the path for your CIFS share when needed. systemd-escape handles hyphens in mount paths, so you don’t have to guess the generated unit name.

What the log saysWhat to check next
Mount timed out or host unreachableNAS availability, routing, firewall, and whether the host’s link dropped during boot.
NFS access deniedExport path and the client address allowed by the NAS.
CIFS permission deniedCredentials file, account permissions, and share access. Check kernel messages for more detail.
Missing mount or unexpected sourceThe fstab entry and the script’s SHARES rows. Compare them with findmnt --mountpoint PATH -o SOURCE,FSTYPE.
Mounted but directory check failedThe share is attached but the read check failed. Investigate NAS responsiveness and permissions before deciding how to recover it.

Reset the three attempts, or remove the timer

Stop the units before clearing the counter:

Terminal window
systemctl stop mount-recovery.timer
systemctl stop mount-recovery.service
rm -f /run/mount-recovery/attempt
systemctl reset-failed mount-recovery.service
systemctl start mount-recovery.timer

On a host already past the 10-minute boot mark, that schedules the first check immediately.

To remove recovery later:

Terminal window
systemctl disable --now mount-recovery.timer
systemctl stop mount-recovery.service
systemctl reset-failed mount-recovery.service
rm /etc/systemd/system/mount-recovery.timer
rm /etc/systemd/system/mount-recovery.service
rm /usr/local/sbin/mount-recovery
systemctl daemon-reload
rm -rf /run/mount-recovery

Your existing fstab entries and mounted shares stay in place. Before rerunning a backup, check its destination again.

Did you came across similar issues? How did you fix it? Let me know in the comment below.

Comments

Loading comments…

← All posts

Type to search the published collection.