我们目前正在迁移到我们的unix环境中的SSH和Kerberos身份验证方案。我需要在脚本被中断,出现错误或脚本成功执行的任何时候,在我们所有的自动化Python脚本中发出Kerberos操作系统命令。我知道在bash中你可以陷入退出,但从我的研究,python中没有这些功能。我尝试使用try/except/else块和工作,但不会捕获直接进程kill并发出命令。我绝不是一个Python专家,所以有人知道一个更好的方法来看看这个函数吗?而且,这只适用于调用主函数的简单脚本,我的一些脚本是面向对象的,而且更复杂,我的方法无法工作。这是我的一个简单的循环尝试。有什么建议?退出时的python陷阱/捕获系统异常并发出kerberos命令
def main():
while (True):
print "Interrupt Me..."
def watchForInterrupts(x):
#define function to issue kdestroy command
def issueKdestroy():
import os
os.system("kdestroy -q")
print "issued kdestroy"
try:
x()
except:
print "interrupted, issuing kdestroy"
#call issueKdestroy function if interrupted
issueKdestroy()
#else block to issue kdestroy if script completed successfully
else:
print "executed successfully, issuing cleanup kdestroy"
issueKdestroy()
#call watchForInterrupts function with main passed as a parameter
watchForInterrupts(main)
寻找'finally'关键字。它的工作原理类似于java :) –