Is there a good way to detect a stale NFS mount

后端 未结 7 1106
别跟我提以往
别跟我提以往 2020-12-23 15:08

I have a procedure I want to initiate only if several tests complete successfully.

One test I need is that all of my NFS mounts are alive and well.

Can I do

相关标签:
7条回答
  • 2020-12-23 15:36

    Took me some time, but here is what I found which works in Python:

    import signal, os, subprocess
    class Alarm(Exception):
        pass
        
    def alarm_handler(signum, frame):
        raise Alarm
    
    pathToNFSMount = '/mnt/server1/' # or you can implement some function 
                                     # to find all the mounts...
    
    signal.signal(signal.SIGALRM, alarm_handler)
    signal.alarm(3)  # 3 seconds
    try:
        proc = subprocess.call('stat '+pathToNFSMount, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) 
        stdoutdata, stderrdata = proc.communicate()
        signal.alarm(0)  # reset the alarm
    except Alarm:
        print "Oops, taking too long!"
    

    Remarks:

    1. credit to the answer here.

    2. You could also use alternative scheme:

      os.fork() and os.stat()

    check if the fork finished, if it has timed out you can kill it. You will need to work with time.time() and so on.

    0 讨论(0)
提交回复
热议问题