使用Fabric在远程shell中运行()调用时,是否可以捕获错误代码? [英] Can I catch error codes when using Fabric to run() calls in a remote shell?

查看:140
本文介绍了使用Fabric在远程shell中运行()调用时,是否可以捕获错误代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,一旦run()调用返回一个非零退出代码,Fabric就会退出。然而,对于一些电话,这是预期的。例如,当无法压缩文件时,PNGOut会返回错误代码2。

Normally Fabric quits as soon as a run() call returns a non-zero exit code. For some calls, however, this is expected. For example, PNGOut returns an error code of 2 when it is unable to compress a file.

目前,我只能通过使用shell逻辑来规避此限制( do_something_that_fails || true do_something_that_fails || do_something_else ),但我宁可将我的逻辑保留在简单的Python(as是织物承诺)。

Currently I can only circumvent this limitation by either using shell logic (do_something_that_fails || true or do_something_that_fails || do_something_else), but I'd rather be able to keep my logic in plain Python (as is the Fabric promise).

有没有办法检查错误代码并对其做出反应,而不是使面料恐慌死亡?我仍然希望其他调用的默认行为,因此通过修改环境来改变它的行为看起来不是一个好的选择(就我而言,只要使用它来告诉它,而不是要死)。

Is there a way to check for an error code and react to it rather than having Fabric panic and die? I still want the default behaviours for other calls, so changing its behaviour by modifying the environment doesn't seem like a good option (and as far as I recall, you can only use that to tell it to warn instead of dying anyway).

推荐答案

您可以通过使用设置来防止非零退出代码中止上下文管理器和 warn_only 设置:

You can prevent aborting on non-zero exit codes by using the settings context manager and the warn_only setting:

from fabric.api import settings

with settings(warn_only=True):
    result = run('pngout old.png new.png')
    if result.return_code == 0: 
        do something
    elif result.return_code == 2: 
        do something else 
    else: #print error to user
        print result
        raise SystemExit()

更新:我的答案已经过时了。见下面的评论。

Update: My answer is outdated. See comments below.

这篇关于使用Fabric在远程shell中运行()调用时,是否可以捕获错误代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆