[英]How do I add timeout while using fabric for below code?
Code is as below. 代码如下。 I will explain here.
我会在这里解释。
I use fabric to check the mount point status for 2 server. 我使用交换矩阵检查2个服务器的安装点状态。 The command is
该命令是
df -h /mnt
The problem of this command is that it may hang there forever, so the fabric code will hang forever. 此命令的问题是它可能永远挂在那里,因此结构代码将永远挂起。 I think some timeout mechanism will fix this issue.
我认为某些超时机制可以解决此问题。 But I did not find it on the fabric doc.
但是我没有在结构文档中找到它。 Is there any setting about this ?
有什么设置吗?
import fabric
from fabric import ThreadingGroup as Group
directory = '/mnt'
group = Group('server1', 'server2', user='someuser', connect_kwargs={'password':'somepassword'})
try:
result = group.run("df -h %s" % directory)
except fabric.exceptions.GroupException as e:
err = e
pass
Option #1: use bash timeout to stop the command timeout 10 df -h
选项#1:使用bash超时来停止命令
timeout 10 df -h
Option #2: use the run
function's timeout
parameter . 选项2:使用
run
功能的timeout
参数 。 timeout is specified in seconds, and a CommandTimeout
exception is raised if the command times out. 以秒为单位指定超时,如果命令超时,则会引发
CommandTimeout
异常。
Group.run
Executes
Connection.run
on all member Connections.执行
Connection.run
所有成员的连接。
and Connection.run
和
Connection.run
wraps an SSH-capable implementation of
invoke.runners.Runner.run
;包装支持SSH的
invoke.runners.Runner.run
实现; see its documentation for details.有关详细信息,请参见其文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.