linux 脚本判断挂载,Linux Shell脚本:如何检测NFS挂载点(或服务器)已经死了?

“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

也许现在这个问题得到了充分的回答:).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值