LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-17-2007, 01:30 AM   #1
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 651

Rep: Reputation: 34
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
 
Old 01-17-2007, 02:33 AM   #2
anotherlinuxuser
Member
 
Registered: Jan 2007
Location: Alberta Canada
Distribution: Fedora/Redhat/CentOS
Posts: 70

Rep: Reputation: 19
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
 
Old 01-17-2007, 06:19 PM   #3
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 651

Original Poster
Rep: Reputation: 34
Thank you anotherlinuxuser

best regards

Asnka
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 05:14 AM
How do I check for directory in bash script? nadavvin Programming 1 09-13-2006 04:03 PM
How to check the bash script syntax? mesh2005 Linux - General 2 04-23-2006 08:22 AM
how to have bash script check whether it's a x session? bkeeper Linux - Newbie 9 02-21-2006 06:31 AM
SCRIPT: check if auto-mount mount-points are still mounted markus1982 Linux - Software 0 05-25-2003 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration