General
cancel
Showing results for 
Search instead for 
Did you mean: 

Verifying an NFS mount is available in a script

 
Highlighted
Super Advisor

Verifying an NFS mount is available in a script

Hi there --

I am writing a script that contains a section that verifies an NFS mount is available in order for the script to continue. The first two commands that I have are the following:

bdf |grep tools
if { ${RC} -eq 0]

I would like to have the script do the following: If the above NFS mount is available, the script can continue. If the remote mount is not available, I would like the script to record the error condition in a log file, and stop execution.

What syntax can I use to accomplish this? Thanks
A Journey In The Quest Of Knowledge
8 REPLIES 8
Highlighted
Honored Contributor

Re: Verifying an NFS mount is available in a script

bdf | grep tools > /dev/null
RC=$?
echo $RC

if [ $RC -eq 0 ]
then
echo successful
else
echo not successful
exit
fi

should work in bourne, posix, bash, ksh...
Highlighted
Honored Contributor

Re: Verifying an NFS mount is available in a script

missed the logfile....

bdf | grep tools > /dev/null
RC=$?
echo $RC

if [ $RC -eq 0 ]
then
echo successful
else
echo not successful >> yourlogfile
exit
fi

should work in bourne, posix, bash, ksh...

Highlighted
Acclaimed Contributor

Re: Verifying an NFS mount is available in a script

Hi Andrew:

Your script could do something like this:

#!/usr/bin/sh
typeset MNT=/tools
typeset LOG=/var/tmp/`basename $0`.log
bdf | grep -q " ${MNT}$" || \
{ echo "$(date '+%x %X') No mountpoint: ${MNT}" >> ${LOG}; exit 1; }
echo "Mountpoint ${MNT} available -- continuing"

...

The LOG file is named with the basename of your script and written into '/var/tmp'. The (q)uiet form of 'grep returns only the exit status which if not zero results in a log entry with a timestamp and the name of the mountpoint that was missing. Note that the 'grep' is anchored to the end of a line with the $-character and has a space before the defined name. This means that you can match and differentiate '/tools', from '/tools2', for example.

Regards!

...JRF...
Highlighted
Acclaimed Contributor

Re: Verifying an NFS mount is available in a script

>bdf |grep tools

Instead of grep, why not do the bdf on the file system in question?

As in JRF's:
bdf ${MNT} || \
Highlighted
Acclaimed Contributor

Re: Verifying an NFS mount is available in a script

Oops, you'll also need to redirect the output:
bdf ${MNT} > /dev/null 2>> ${LOG} || \
Highlighted
Frequent Advisor

Re: Verifying an NFS mount is available in a script

Hi. Just one thing that I like to use myself.
If you still want to use grep on the mounted File systems and not running BDF on a specific mount, I allways prefer to grep in the /etc/mnttab file since the BDF has some output issues ( devides the output to more than one line sometimes ) and it runs faster.
By the way can anyone think of a reason why the grep in /etc/mnttab may give a different result than the bdf ( I mean an issue where mnttab thinks FS is mounted but it is accutaly not or something like that) ?

Leonid.
Highlighted
Honored Contributor

Re: Verifying an NFS mount is available in a script

> By the way [...]

As "man mnttab" says, "/etc/mnttab should
never be manually edited", but I've seen it
done. Normally, it's pretty safe, however.
Highlighted
Exalted Contributor

Re: Verifying an NFS mount is available in a script

Shalom,

If the NFS mount fails bdf will hang, making the script output of little use because you won't get anything.

I would use a combination of showmount -e hostname (it can be localhost) and a /etc/fstab or the /etc/mnttab file to confirm that NFS mounts are valid.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com