Search code examples
linuxbashshellmountnfs

Check if NFS Directory Mounted without Large Hangs on Failure


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!


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