Introduction:
Every now and then if an NFS mount is no more connected to the server or something goes wrong with the NFS connection, running the command ‘ls mountpoint’ hangs the terminal till I press CTRL-C. So I tried to figure out a script that will be run as cron job and will tell me when an NFS mount has gone wrong. I had to revert to unorthodox tricks since doing a simple command ‘stat mountpoint &’ within the script would also hang the script. So I use the command ‘at now’ which runs the command independently for the script that initiated it. Here is an example of such script.

#!/bin/bash
# Name: MOUNT_CHECK.sh
# Purpose: Checks the health of the NFS mountpoint given by argument
# it kills the at/stat process and exits with an exit code 2 if the timeout has expired.
#-------------------------------------------------------------------
startdelay=3
timeout=10
# processes to be excluded in the 'ps | grep' test
excludes="openvpn|istatd|rpc.statd"
if [ $# -ne 1 ]; then
echo "ERROR: Needs mountpoint as argument"
echo "Usage: MOUNT_CHECK.sh MountPoint"
exit 2
fi
#
echo "/usr/bin/stat $1" | /usr/bin/at now
sleep $startdelay
while (ps ax | egrep -v "grep|$excludes" | grep -q stat); do
let count=${count}+1
sleep 1
if [ $count -ge $timeout ]; then
kill $(pidof stat)
#echo "Mountpoint $1 : FAILED to connect before timeout of $timeout sec."
exit 2
fi
done