Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top