check an nfs mount in a script

Hi,
Ive been looking for a way to do this but cant figure it out.

I have a script that checks an nfs mount with this condition (linux
bash) and makes sure its not full:

dirpresent=`df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%"`
if [ -n "$dirpresent" ]; then
etc etc
fi

My problem is if the nfs server is down, then the df command hangs so
the whole script hangs. Is there a way I can time the reponse so if
say I dont get a response after 5 seconds, then abort the df check?

Thanks
0
cconnell_1
7/3/2008 10:04:40 PM
comp.unix.shell 15415 articles. 1 followers. Post Follow

5 Replies
1423 Views

Similar Articles

[PageSpeed] 55

On Thu, 03 Jul 2008 19:04:40 -0300, <cconnell_1@lycos.com> wrote:

> Hi,
> Ive been looking for a way to do this but cant figure it out.
>
> I have a script that checks an nfs mount with this condition (linux
> bash) and makes sure its not full:
>
> dirpresent=`df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%"`
> if [ -n "$dirpresent" ]; then
> etc etc
> fi
>
> My problem is if the nfs server is down, then the df command hangs so
> the whole script hangs. Is there a way I can time the reponse so if
> say I dont get a response after 5 seconds, then abort the df check?
>
> Thanks
> 


Try this:

read -t5 dirpresent < <(df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%")
0
mop2
7/3/2008 11:29:09 PM
On 4 Jul, 00:29, mop2 <inva...@mail.address> wrote:
> On Thu, 03 Jul 2008 19:04:40 -0300, <cconnel...@lycos.com> wrote:
> > Hi,
> > Ive been looking for a way to do this but cant figure it out.
>
> > I have a script that checks an nfs mount with this condition (linux
> > bash) and makes sure its not full:
>
> > dirpresent=`df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%"`
> > if [ -n "$dirpresent" ]; then
> > etc etc
> > fi
>
> > My problem is if the nfs server is down, then the df command hangs so
> > the whole script hangs. Is there a way I can time the reponse so if
> > say I dont get a response after 5 seconds, then abort the df check?
>
> > Thanks
>
> Try this:
>
> read -t5 dirpresent < <(df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%")- Hide quoted text -
>
> - Show quoted text -


thanks this worked well. one side effect is after the script it leaves
the df -t commands around and also the script says its running from
ps.
is there any way to terminate the df or send a control-c to it after
the timeout?

0
cconnell_1
7/4/2008 12:30:50 AM
On Thu, 03 Jul 2008 21:30:50 -0300, <cconnell_1@lycos.com> wrote:

> On 4 Jul, 00:29, mop2 <inva...@mail.address> wrote:
>> On Thu, 03 Jul 2008 19:04:40 -0300, <cconnel...@lycos.com> wrote:
>> > Hi,
>> > Ive been looking for a way to do this but cant figure it out.
>>
>> > I have a script that checks an nfs mount with this condition (linux
>> > bash) and makes sure its not full:
>>
>> > dirpresent=`df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%"`
>> > if [ -n "$dirpresent" ]; then
>> > etc etc
>> > fi
>>
>> > My problem is if the nfs server is down, then the df command hangs so
>> > the whole script hangs. Is there a way I can time the reponse so if
>> > say I dont get a response after 5 seconds, then abort the df check?
>>
>> > Thanks
>>
>> Try this:
>>
>> read -t5 dirpresent < <(df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%")- Hide quoted text -
>>
>> - Show quoted text -
>
>
> thanks this worked well. one side effect is after the script it leaves
> the df -t commands around and also the script says its running from
> ps.
> is there any way to terminate the df or send a control-c to it after
> the timeout?
>
> 


If the df survived, this may be a better solution:

if mount|grep -q ' nfs ' && df -T | grep -w nfs | grep -w $nfsdir | grep -q -wv "100%";then
etc etc
0
mop2
7/4/2008 12:50:01 AM
On Thu, 03 Jul 2008 17:30:50 -0700, cconnell_1 wrote:

> On 4 Jul, 00:29, mop2 <inva...@mail.address> wrote:
>> On Thu, 03 Jul 2008 19:04:40 -0300, <cconnel...@lycos.com> wrote:
>> > Hi,
>> > Ive been looking for a way to do this but cant figure it out.
>>
>> > I have a script that checks an nfs mount with this condition (linux
>> > bash) and makes sure its not full:
>>
>> > dirpresent=`df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%"`
>> > if [ -n "$dirpresent" ]; then
>> > etc etc
>> > fi
>>
>> > My problem is if the nfs server is down, then the df command hangs so
>> > the whole script hangs. Is there a way I can time the reponse so if
>> > say I dont get a response after 5 seconds, then abort the df check?
>>
>> > Thanks
>>
>> Try this:
>>
>> read -t5 dirpresent < <(df -T | grep -w nfs | grep -w $nfsdir | grep
>> -wv "100%")- Hide quoted text -
>>
>> - Show quoted text -
> 
> 
> thanks this worked well. one side effect is after the script it leaves
> the df -t commands around and also the script says its running from ps.
> is there any way to terminate the df or send a control-c to it after the
> timeout?

Another way of doing this is with http://stromberg.dnsalias.org/~strombrg/
maxtime.html  It's specifically written with NFS mounts in mind.

However, on a hard,nointr mount, maxtime will leave processes behind too, 
though they should die as soon as the mount comes back.

To get around that, you'll probably need to change the mount options on 
your NFS mount - probably in /etc/fstab, /etc/vfstab, /etc/checklist, or 
an automount map.  You could probably change it to soft (mount will time 
out and disappear after a while) or intr (processes attempting to use the 
mount are interruptible even if the NFS mount is timing out).

0
Dan
7/4/2008 12:53:15 AM
cconnell_1@lycos.com writes:

> dirpresent=`df -T | grep -w nfs | grep -w $nfsdir | grep -wv "100%"`
> if [ -n "$dirpresent" ]; then
> etc etc
> fi
>
> My problem is if the nfs server is down, then the df command hangs so
> the whole script hangs. Is there a way I can time the reponse so if
> say I dont get a response after 5 seconds, then abort the df check?


Decades ago when I had to deal with this (we had 100's of NFS file
servers), I seem to recall it was better to launch a job in the
background, if an NFS job might hang it up.

So it might be better to have a loop run the "df" command in the
background, and in another shell check the output. 

I did two other things to reduce the risk of server hanging:
  1) Keeping excess directories out of my searchpath

  2) creating a directory on the local machine that had symbolic links
  to the executables on remote diorectories, and use this directory in
  my searchpath instead of the other).

0
Maxwell
7/5/2008 11:45:04 AM
Reply: