我們目前正在遷移到我們的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 :) –