In a bourne shell script (#!/bin/sh) how can check to see if a remote NFS share is mounted and, if it is not, mount it? I've got an ugly set of cat, greps and ifs using the output of 'mount' at the moment but it doesn't seem to be doing a reliable job.
Follow
3,67111 gold badge2121 silver badges1919 bronze badges
asked Jul 15, 2009 at 6:20
7,3162424 gold badges8484 silver badges109109 bronze badges
-
2
It would be helpful to see your own effort in solving this. If you have a code that's unreliable, show it here and help us make it reliable. CommentedJul 15, 2009 at 6:27
9 Answers
Sorted by:
Highest score (default) Date modified (newest first) Date created (oldest first)
5
If possible, setting up automount ( autofs ) would be the standard way to do this. It might already be in your distribution (comes with CentOS / Redhat default install ). Here is a tutorial.
Why use Automount?
Automounting is the process where mounting and unmounting of certain filesystems is done automatically by a daemon. If the filesystem is unmounted, and a user attempts to access it, it will be automatically (re)mounted. This is especially useful in large networked environments and for crossmounting filesystems between a few machines (especially ones which are not always online).
Follow
answered Jul 15, 2009 at 11:30
85.8k7777 gold badges318318 silver badges463463 bronze badges
-
1
/facepalm. Now that you mention it automount is such an obvious way to approach this! CommentedJul 16, 2009 at 6:02
Report this ad
3
Can you grep /etc/mtab for the device? grep -c '/mnt/foo' /etc/mtab if grep outputs '1' then /mnt/foo is mounted.
Follow
answered Jul 15, 2009 at 6:47
39622 silver badges99 bronze badges
- That'd be basically the same as what he/she is currently doing, grepping mount's output.
– derobert
CommentedJul 15, 2009 at 6:49 -
2
Ok, but why is it not reliable? Bug in the script or problem with mount's output?– beggs
CommentedJul 15, 2009 at 6:53 -
1
what about greping /proc/mounts? on many sytems this is more up to date than /etc/mtab CommentedMay 21, 2010 at 22:22
3
Use mountpoint.
mountpoint -q /path/to/nfs/share || mount -t nfs server:/nfs/share /path/to/nfs/share
(I don't know how widespread or portable mountpoint is; it's provided by the initscripts package on my Debian server.)
Follow
answered Jul 15, 2009 at 12:45
99211 gold badge77 silver badges1717 bronze badges
- At least it exists in fedora 17 - and was unaware of it. Thanks! CommentedSep 7, 2012 at 16:49
Report this ad
2
In solaris
If your checking that the system where the script is running has a remote filesystem mounted then
ISMOUNTED=`/usr/sbin/mount | grep "^/path/to/mount "`
if [ "$ISMOUNTED" = "" ]
then
mountcommand*
fi
*mountcommand could be /usr/sbin/mount /path/to/mount if there is a corresponding entry in the /etc/vfstab or /usr/sbin/mount remotehost:/remote/path /path/to/mount
Follow
community wiki
1
You may be able to do something with stat. The "device" field will be different across different filesystems. So, assuming you want to see if /mnt/foo is mounted, you'd compare the output of stat -c%d /mnt/ to stat -c%d /mnt/foo/. If the device is different, something is mounted there.
if [ `stat -c%d /mnt/` -eq `stat -c%d /mnt/foo/` ]; then
mount /mnt/foo
fi
Follow
answered Jul 15, 2009 at 6:37
1,3281212 silver badges2323 bronze badges
1
When it comes down to it, shell programming is about plugging together small discrete tools using pipes to produce some kind of compound utility. A utility that did what you're asking for in a "smart" way wouldn't really match the Unix philosophy.
If you want to do it more intelligently, you might want to look at doing this in Perl or Python or C, where you can use the library functions to talk to the portmapper to get information about mounted filesystems as a data structure. You can then intelligently perform the tasks to change the current state to the state you want.
Follow
answered Jul 15, 2009 at 8:15
6,91911 gold badge2828 silver badges2424 bronze badges
1
Just to throw another idea out there, the df command can tell you the mounted filesystem of a directory. If you throw in the -l option, you get a pretty easy test to see if a directory is on a local filesystem or not.
$ cd /net/remoteshare
$ df -l .
df: no file systems processed
$ echo $?
1
Follow
answered Jul 15, 2009 at 13:30
2,15611 gold badge1616 silver badges1414 bronze badges
0
A few suggestions:
-
If your distribution has an RC script to handle NFS mounts then it would be prudent to use or it check it's status. You can run into trouble if you incorrectly assume that services such as
portmapandstatdare already started. -
It's often more reliable to use
/proc/mountsin favour of output frommountor the possibly out-of-date content of/etc/mtab. -
Use
grep -qsand check the return code to determine whether a mount is present or not. -
Assuming that all of the NFS mounts listed in
/etc/fstabshould be mounted then you can mount them universally withmount -a -t nfs,nfs4. Anything already mounted will be ignored.
Follow
answered Jul 15, 2009 at 8:37
26.1k55 gold badges5656 silver badges7171 bronze badges
0
#!/bin/bash
mountpoint='your mountpoint'
mobileno=""
smsurl=""
mntcmd='/bin/mount -a'
mntchkcmd='/bin/mount'
###Check mount point available or not IF not alert to service own
${mntchkcmd} | /bin/grep ${mountpoint} > /dev/null 2>&1
if [ $? != 0 ]
then
${mntcmd}
sleep 5
${mntchkcmd} | /bin/grep ${mountpoint} > /dev/null 2>&1
if [ $? != 0 ]
then
echo "issue with mount point"
exit
fi
fi
###If mount point is available then check it for utilisation.
warn=90
critical=95
disksize=$(/bin/df -k ${mountpoint} | /bin/grep ${mountpoint} | /bin/awk -F% '{print $1}' | /bin/awk '{print $4}')
if [ ${disksize} -gt ${warn} ]
then
echo 'warn'
elif [ ${disksize} -gt ${critical} ]
then
echo 'critical'
fi

251

被折叠的 条评论
为什么被折叠?



