現在、UNIX 環境で SSH および Kerberos 認証方式に移行中です。スクリプトが中断されたとき、エラーが発生したとき、またはスクリプトが正常に実行されたときはいつでも、すべての自動化された Python スクリプトで Kerberos OS コマンドを発行する必要があります。bashでは終了時にトラップできることは知っていますが、私の調査によると、Pythonにはその機能がありません。私の試みはtry/except/elseブロックであり、機能しますが、直接のプロセスキルをキャッチしてコマンドを発行しません。私は決してPythonの専門家ではないので、これに対するより良いアプローチや調べる関数を知っている人はいますか? また、これはメイン関数が呼び出される単純なスクリプトでのみ機能します。私のスクリプトの一部はオブジェクト指向でより複雑であり、私のアプローチは機能しません。これが単純なループでの私の試みです。何かアドバイス?
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)