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.
-
2It 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. – Karolis T. Jul 15 '09 at 06:27
9 Answers
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).

- 83,619
- 74
- 305
- 448
-
1/facepalm. Now that you mention it automount is such an obvious way to approach this! – DrStalker Jul 16 '09 at 06:02
Can you grep /etc/mtab
for the device? grep -c '/mnt/foo' /etc/mtab
if grep outputs '1' then /mnt/foo is mounted.

- 396
- 2
- 9
-
That'd be basically the same as what he/she is currently doing, grepping mount's output. – derobert Jul 15 '09 at 06:49
-
2Ok, but why is it not reliable? Bug in the script or problem with mount's output? – beggs Jul 15 '09 at 06:53
-
1what about greping /proc/mounts? on many sytems this is more up to date than /etc/mtab – user12096 May 21 '10 at 22:22
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.)

- 983
- 1
- 7
- 17
-
At least it exists in fedora 17 - and was unaware of it. Thanks! – David Ramirez Sep 07 '12 at 16:49
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

- 115,471
- 20
- 215
- 297
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

- 1,308
- 12
- 22
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.

- 6,689
- 1
- 26
- 24
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

- 2,116
- 1
- 16
- 14
#!/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

- 17,911
- 6
- 63
- 82
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
portmap
andstatd
are already started.It's often more reliable to use
/proc/mounts
in favour of output frommount
or the possibly out-of-date content of/etc/mtab
.Use
grep -qs
and check the return code to determine whether a mount is present or not.Assuming that all of the NFS mounts listed in
/etc/fstab
should be mounted then you can mount them universally withmount -a -t nfs,nfs4
. Anything already mounted will be ignored.

- 25,617
- 5
- 53
- 70