Remount CIFS on network reconnect

Solution 1:

Your script seems like a viable option to me. I've seen far more convoluted workarounds to issues in Linux.

Another alternative is a systemd automount which fulfills the or at least mounting automatically upon access requirement. Simple enough to implement and revert back if it's not exactly what you want.

[1] Unmount the share if it is already mounted:

sudo umount /mnt/MyShare

[2] Add 3 more options to your fstab declaration: noauto,x-systemd.automount,x-systemd.idle-timeout=30

//192.168.0.100/MyShare  /mnt/MyShare  cifs  username=<username>,password=<password>,rw,uid=1000,gid=1000,nounix,iocharset=utf8,file_mode=0770,dir_mode=0770,vers=3.0,noauto,x-systemd.automount,x-systemd.idle-timeout=30  0  0

Then do some systemd stuff:

sudo systemctl daemon-reload
sudo systemctl restart remote-fs.target

The share will not mount at boot ( noauto ) but when the mount point is accessed ( noauto,x-systemd.automount ) and it will automatically unmount if share access is idle for more than 30 seconds - user adjustable ( x-systemd.idle-timeout=30 )