Managing Pesky NFS Mounts With A Shell Script

I have used NFS[1] mounts in our SOHO for many years for personal and business storage of files that need to be accessed from more than one PC. The file systems in /mnt are symbolically liked to /home/user directories and called mounts here. This makes accessing them very easy from each user account on a Linux PC. This is great when it works, not so great when one of the shares goes belly up. When one NFS share stops responding new access to all connected file systems in /mnt is likely to be nonexistent and cause applications trying to access the /home/user/mounts directory to hang. I have been manually removing each mounted NFS share with umount -f and then restarting them one at a time with mount over the years. I know, I know, I know – I should have studied the man(ual)[2] pages instead of doing that.

Today I did study the man pages for mount and umount because I was tired of manually dismounting and remounting all these shares periodically when one of them died and blocked my access to all my mounted file systems in /mnt. After all, I thought, there are likely situations where locations have dozens of mounted NFS shares and need to reset one or more. This has to have been addressed with a way to handle multiple shares with a single command. In other words, I figured there had to be a better way and I was correct.

What I found were umount -a -f -t nfs and mount -a -t nfs which do in a couple of commands what I had been doing tediously by hand one at a time for several NFS shares. Now, I am aware that this is a shotgun approach that does not take into account open files on these shares. On a multiuser system with multiple NFS shares on other systems one would need to be more aware of and cautious of others that may be actively using NFS shares. But in my case these are connections for personal desktop use and I can kill them off without worry that I will affect someone other than myself.

I still wanted to reduce what I had to type at the CLI (Command Line Interface) to do this so I wrote a script that I placed in /root/bin to run at need. Here is the script:

echo "Remount remote NFS shares."
echo "Forcing umount of remote NFS shares ..."
umount -a -f -t nfs
mount|grep "type nfs"
echo "Sleeping 5 seconds ...";sleep 5;echo "Mounting remote NFS shares ..."
mount -a -t nfs
echo "Listing remote NFS shares ..."
mount|grep "type nfs"

Study that and see if you can figure out what it does by looking at the man pages for mount and umount. I named this remountnfs and I ran chmod 750 /root/bin/remountnfs to make it executable from the CLI. Now all I need to do to reset those nasty hung NFS shares is type su -c /root/bin/remountnfs at the CLI and supply my root password to reset those shares and make /mnt accessible once more.

  • [1] If you have no idea what NFS happens to be then read What is NFS from the Linux NFS-HOWTO.
  • [2] Anyone that really wants to be a Linux Power User should learn to study and decipher man pages. Start by studying man man and go from there. Almost every command one can type at the CLI has a corresponding man page.

Have a suggestion as to how I could do this better? Leave a comment.


Published by

Gene A.

Gene is a "Unix Guy", network technologist, system trouble-shooter and IT generalist with over 20 years experience in the SOHO and SMB markets. He is familiar with and conversant in eComStation (a.k.a. OS/2), DOS (PC, MS and Free), Unix, Linux and those GUI based systems from Microsoft. Gene is also a follower of Jesus (forgiven, not perfect), and this does inform his world view.