Check python script if NFS server is installed and online

I have a question similar to Check if the NFS share is installed in the python script , however in my case the NFS server is installed, but the server crashed and went offline. I tried to take this into account using os.dir.ismount('/path/to/mountpoint/) , however my problem is that this command terminates forever.

When I try to do a simple ls /path/to/mountpoint , this command also does not end. Is there something fundamentally wrong? My setup is a Linux NFSv3 server and a Linux NFS client.

Usually, I expect that if the NFS server cannot yet be unavailable or unavailable, the ls displays the contents of the local directory, rather than stopping the terminal.

+4
source share
1 answer

This happens with NFS, if you set up an NFS share and the server is no longer available on the network, it often happens that any IO access to the loop simply gets stuck. If you try to run ls in the terminal, you will see that it just got stuck, does not fail, it just waits for an endless answer that it will never get. So, I recommend that you execute the ls command in your Python code and then set the timeout. Once this timeout is reached, you can throw an exception. Below is the implementation I tested. call_timeout is the function you give to the command you want to execute and the wait period in seconds. If the command completes before the timeout, it returns immediately, otherwise it kills the process that it spawned and raises an OSError that you can catch. Instead, you could choose True or False, this is a design choice. Some sample calls are listed below.

code

 from subprocess import Popen, check_output import time def call_timeout(cmd, timeout): start = time.time() p = Popen(cmd) while time.time() - start < timeout: if p.poll() is not None: return time.sleep(0.1) p.kill() raise OSError('command timed out') 

Call Examples

this should return without errors:

 call_timeout(["sleep", "1"], 0.5) 

this will throw an OSError error:

 call_timeout(["sleep", "1"], 1.5) 

you need something in this direction:

 call_timeout(["ls", "/path/to/mountpoint"], 5.0) 
+7
source

All Articles