0

I'm writing a shell script to check whether or not certain nfs mounts can be seen by nodes in a cluster.

The script works by doing an ls /nfs/"machine" |wc -l and if it's greater than 0 it will pass the test. My main concern with this solution is how long ls will hang if a disk is not mounted.

I had a go at the solution in this question "bash checking directory existence hanging when NFS mount goes down" but the results did not correspond with what was actually mounted.

I also tried doing a df -h /nfs/"machine" but that has a large hang if the disk isn't mounted.

Basically, is there an alternate way which can let me know if a disk is mounted or not without large hangs?

Alternatively, is there a way of restricting the time that a command can be executed for?

Thanks in advance!

Community
  • 1
  • 1
stuts
  • 45
  • 2
  • 5

2 Answers2

2

Ok I managed to solve this using the timeout command, I checked back here to see that BroSlow updated his answer with a very similar solution. Thank you BroSlow for your help.

To solve the problem, the code I used is:

if [[ `timeout 5s ls /nfs/machine |wc -l` -gt 0 ]] ; then
      echo "can see machine"
else
      echo "cannot see machine"
fi

I then reduced this to a single line command so that it could be run through ssh and put inside of a loop (to loop through hosts and execute this command).

stuts
  • 45
  • 2
  • 5
0

Couple of possibilities:

1)

find /nfs/machine -maxdepth 0 -empty should be a lot faster than ls /nfs/machine, though I'm not sure that's the problem in this case (also not sure sleep is needed, but might be some offset.

if [[ $(find /nfs/machine -maxdepth 0 -empty 2> /dev/null) == "" ]]; then
  sleep 1 && [[ $(mount) == *"/nfs/machine"* ]] && echo "mounted" || echo "not mounted"
else
  echo "mounted"
fi

2)

timeout 10 ls -A /nfs/machine | wc -l
if [[ $? > 0 ]]; then
  echo "mounted"
else
  echo "not mounted"
fi
Reinstate Monica Please
  • 11,123
  • 3
  • 27
  • 48
  • I see, I tried your solution and the main problem that I've come across with it is if the cluster hasn't accessed that disk yet, your command fails, but after an ls /nfs/machine your command is a success. So there's a chance that this could fail even if /nfs/machine can be accessed by the cluster nodes. My test is to see whether or not the disk can be mounted rather than if it is mounted at the present moment. – stuts Jan 14 '14 at 12:58
  • @stuts edited a couple of other possibilities, do either of those work for you (change `timeout` to something reasonable in seconds or append `m`, etc... for minutes) – Reinstate Monica Please Jan 14 '14 at 14:46