如何防止强制退出Python脚本?

时间:2015-04-08 19:27:56

标签: python linux unix

我正在运行python脚本并尝试阻止强制退出或打印错误消息或将其记录到日志文件中。

我已经使用logging.info("")进行日志记录..或仅使用print进行打印..

但是什么或如何创建一个方法或函数,可以在强制退出时进行打印或记录?

例如,如果我的test.py正在运行,我按 Ctrl + C 退出..我想记录或打印出来..

signal.signal(signal.SIGUSR1, handler)
logging.info("Checked for signal to stop")
if stop:
        logging.info("Inside of main if loop for stop signal")
        logging.info("Stop signal captured. Exiting the program")
        smtpObj.sendmail(sender, receivers, message + "Stop signal captured. Exiting the program")
        sys.exit("EXIT SIGNAL CAPTURED: EXITING")

我想在退出程序时使用上面的编码进行记录。 但这并不涉及像ctrl + c这样的东西我想记录以防万一程序意外退出

2 个答案:

答案 0 :(得分:1)

<强>已更新

使用try和except:

try:
    signal.signal(signal.SIGUSR1, handler)
    logging.info("Checked for signal to stop")
    if stop:
        logging.info("Inside of main if loop for stop signal")
        logging.info("Stop signal captured. Exiting the program")
        smtpObj.sendmail(sender, receivers, message + "Stop signal captured.     Exiting the program")
        sys.exit("EXIT SIGNAL CAPTURED: EXITING")
except KeyboardInterrupt as kbe:
    log.info(str(kbe))

您还可以利用atexit模块在​​脚本退出时执行函数。

import atexit

def alldone():
    log.warning('Something went wrong')

# your code here...

https://docs.python.org/2/library/atexit.html

答案 1 :(得分:0)

你应该看看Python Signal Handlers(特别是signal.CTRL_C_EVENT)。

以下是一个更完整的答案...注意:您可以通过使用此类内容使您的流程完全无法收回。

#!/usr/bin/env python

import signal, os
import time

def handler( signum, frame ):
  print "Signal handler caught", signum, "@", time.ctime()
  if signum == 2:
    raise "Caught Signal 2 - Exiting"

#
# Windows Supported Values
#
#signal.signal( signal.SIGABRT, handler )
#signal.signal( signal.SIGFPE, handler )
#signal.signal( signal.SIGILL, handler )
#signal.signal( signal.SIGINT, handler )
#signal.signal( signal.SIGSEGV, handler )
#signal.signal( signal.SIGTERM, handler )

#
# UNIX Supported Values (YMMV)
#
signal.signal( signal.SIGABRT, handler )
signal.signal( signal.SIGALRM, handler )
signal.signal( signal.SIGBUS, handler )
signal.signal( signal.SIGCHLD, handler )
signal.signal( signal.SIGCLD, handler )
signal.signal( signal.SIGCONT, handler )
signal.signal( signal.SIGFPE, handler )
signal.signal( signal.SIGHUP, handler )
signal.signal( signal.SIGILL, handler )
signal.signal( signal.SIGINT, handler )
signal.signal( signal.SIGIO, handler )
signal.signal( signal.SIGIOT, handler )
#signal.signal( signal.SIGKILL, handler )
signal.signal( signal.SIGPIPE, handler )
signal.signal( signal.SIGPOLL, handler )
signal.signal( signal.SIGPROF, handler )
signal.signal( signal.SIGPWR, handler )
signal.signal( signal.SIGQUIT, handler )
signal.signal( signal.SIGRTMAX, handler )
signal.signal( signal.SIGRTMIN, handler )
signal.signal( signal.SIGSEGV, handler )
#signal.signal( signal.SIGSTOP, handler )
signal.signal( signal.SIGSYS, handler )
signal.signal( signal.SIGTERM, handler )
signal.signal( signal.SIGTRAP, handler )
signal.signal( signal.SIGTSTP, handler )
signal.signal( signal.SIGTTIN, handler )
signal.signal( signal.SIGTTOU, handler )
signal.signal( signal.SIGURG, handler )
signal.signal( signal.SIGUSR1, handler )
signal.signal( signal.SIGUSR2, handler )
signal.signal( signal.SIGVTALRM, handler )
signal.signal( signal.SIGWINCH, handler )
signal.signal( signal.SIGXCPU, handler )
signal.signal( signal.SIGXFSZ, handler )
#signal.signal( signal.SIG_DFL, handler )
signal.signal( signal.SIG_IGN, handler )


print time.ctime(), " - Started"

while True:
  time.sleep( 1 )
  print " Tick:", time.ctime()