“stat”命令是一种更清洁的方式:
statresult=`stat /my/mountpoint 2>&1 | grep -i "stale"`
if [ "${statresult}" != "" ]; then
#result not empty: mountpoint is stale; remove it
umount -f /my/mountpoint
fi
此外,您可以使用rpcinfo来检测远程nfs共享是否可用:
rpcinfo -t remote.system.net nfs > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo Remote NFS share available.
fi
新增2013-07-15T14:31:18-05:00:
我进一步研究了这个问题,因为我还在研究需要识别过时挂载点的脚本.受到one of the replies的启发,“有没有一种很好的方法可以检测过时的NFS挂载”,我认为以下可能是检查bash中特定挂载点陈旧性的最可靠方法:
read -t1 <
if [ $? -eq 1 ]; then
echo NFS mount stale. Removing...
umount -f -l /my/mountpoint
fi
如果stat命令由于某种原因挂起,则“read -t1”构造可靠地超出子shell.
新增2013-07-17T12:03:23-05:00:
虽然读-t1< / dev / null 2>& 1在子shell中,或在命令行的末尾打破它.使用简单的测试:if [-d / path / to / mountpoint];然后… fi也可以工作,并且在脚本中可能更好.经过大量测试后,我最终使用了它.
新增2013-07-19T13:51:27-05:00:
对我的问题“How can I use read timeouts with stat?”的回复提供了关于当目标不可用时将stat(或rpcinfo)的输出静音的额外细节,并且命令在它自己超时之前挂起几分钟.虽然[-d / some / mountpoint]可用于检测陈旧的挂载点,但rpcinfo没有类似的替代方法,因此使用read -t1重定向是最佳选择.子shell的输出可以用2>& – 静音.以下是CodeMonkey’s response的示例:
mountpoint="/my/mountpoint"
read -t1 < &-)
if [[ -n "$REPLY" ]]; then
echo "NFS mount stale. Removing..."
umount -f -l "$mountpoint"
fi
也许现在这个问题得到了充分的回答:).