Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
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. –  Karolis T. Jul 15 '09 at 6:27
add comment

9 Answers

up vote 5 down vote accepted

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).

share|improve this answer
1  
/facepalm. Now that you mention it automount is such an obvious way to approach this! –  DrStalker Jul 16 '09 at 6:02
add comment
#!/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
share|improve this answer
1  
check mountpoint and its usage script. –  Tanaji Yadav Sep 23 '11 at 11:09
add comment

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

share|improve this answer
add comment

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.)

share|improve this answer
    
At least it exists in fedora 17 - and was unaware of it. Thanks! –  David Ramirez Sep 7 '12 at 16:49
add comment

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

share|improve this answer
add comment

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 and statd are already started.

  • It's often more reliable to use /proc/mounts in favour of output from mount 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 with mount -a -t nfs,nfs4. Anything already mounted will be ignored.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Can you grep /etc/mtab for the device? grep -c '/mnt/foo' /etc/mtab if grep outputs '1' then /mnt/foo is mounted.

share|improve this answer
    
That'd be basically the same as what he/she is currently doing, grepping mount's output. –  derobert Jul 15 '09 at 6:49
2  
Ok, but why is it not reliable? Bug in the script or problem with mount's output? –  beggs Jul 15 '09 at 6:53
1  
what about greping /proc/mounts? on many sytems this is more up to date than /etc/mtab –  user12096 May 21 '10 at 22:22
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.