LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash script to check mount is ok (http://www.linuxquestions.org/questions/programming-9/bash-script-to-check-mount-is-ok-520051/)

procfs 01-17-2007 02:30 AM

bash script to check mount is ok
 
Hi

My OS is RedHat AS 3, and I have a windows share mounted on this RedHat machine, using mount –t smbfs.

What I want to do is to write a bash shell script to


Check the mount status of filesystem X

If ok (mounted)

Then CKOK=yes

Else

run ping <windows server name>

If ping OK then
Do mount –a
Go back to the beginning and check the mount

Else
Echo “Host unreacable”
fi

fi


Thank you

REgards

Asanka

anotherlinuxuser 01-17-2007 03:33 AM

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

procfs 01-17-2007 07:19 PM

Thank you anotherlinuxuser

best regards

Asnka


All times are GMT -5. The time now is 06:11 AM.