Here is script that should do you want:
Cut and paste the following text into a script name of your choosing, run 'chmod +x scriptname' so it will run.
#!/bin/bash
# Script to check for mounted filesystem
#
# MOUNTSHARE can be SMB: //hostname/sharename, NFS: hostname:/nfsshare/path,
# or a local disk: /dev/hdc1, etc.
# Be sure to set execute bit on file permissions.
MAXTRIES=3
TRIES=0
CKOK=""
PING=""
SHAREHOST=""
if [ "$1" = "" ]; then
# The share to look for can be either hardcoded here
MOUNTSHARE="//somehostname/someshare"
else
# The share can passed as an arg, as in:
# chk_mount //someserver/someshare
MOUNTSHARE="$1"
fi
until [ "$TRIES" -ge "$MAXTRIES" ]; do
TRIES=`expr $TRIES + 1`
MNTDIR=`mount 2> /dev/null | grep "^$MOUNTSHARE " | cut -d' ' -f3`
if [ "$MNTDIR" != "" ]; then # Share is not mounted
echo -e "Share $MOUNTSHARE is mounted on $MNTDIR."
CKOK="yes"
TRIES="$MAXTRIES"
continue
else
echo -e "Share $MOUNTSHARE is not mounted. Try count=$TRIES"
# See if share is SMB, NFS, or Local Disk/Device.
echo $MOUNTSHARE | grep "[a-z].*:/" > /dev/null 2>&1
if [ $? -eq 0 ]; then
# NFS Share
SHAREHOST=`echo $MOUNTSHARE | cut -d':' -f1`
else
echo $MOUNTSHARE | grep "^//" > /dev/null 2>&1
if [ $? -eq 0 ]; then
# SMB Share
SHAREHOST=`echo $MOUNTSHARE | cut -d'/' -f3`
else
echo $MOUNTSHARE | grep "^/dev/" > /dev/null 2>&1
if [ $? -eq 0 ]; then
# Local Device, no host to ping, so skip ping.
HOSTOK="y"
fi
fi
fi
# See if share host is alive.
if [ "$SHAREHOST" != "" ]; then
# Send only 2 pings
ping -c 2 $SHAREHOST 2> /dev/null | grep " 0%" > /dev/null 2>&1
if [ $? -eq 0 ]; then
HOSTOK="y"
echo -e "Host $SHAREHOST is alive."
else
echo -e "No response from host $SHAREHOST."
echo -e "Giving up."
exit 1
fi
fi
if [ "$HOSTOK" = "y" ]; then # Try mounting
mount -a
if [ $? -eq 0 ]; then
echo -e "Auto mount appears successful, rechecking.\n"
else
echo -e "Auto mount appears unsuccessful, rechecking.\n"
fi
fi
fi
done
|