1

I am running a python script and trying to prevent from force exiting or print an error message or log that into the log file.

I am already using logging.info("") for logging.. or just print for printing something..

but what or how do I create a method or function that can either do print or log when it force exit?

For example, if my test.py is running and I press Ctrl + C to exit out.. I want to log that or print out..

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")

I am using above coding for logging for when I want to exit the program. But this doesn't deal with something like ctrl + c I want to also log just in case program exit by accident or something

4
  • Are you wanting to log other exits, or just when ctrl + c is used? Commented Apr 8, 2015 at 19:31
  • You want to read about python exception handling. docs.python.org/3/tutorial/errors.html Commented Apr 8, 2015 at 19:32
  • It would be more helpful if you told us why your code is "force exiting" Commented Apr 8, 2015 at 19:42
  • Take a look to atexit too docs.python.org/2/library/atexit.html Commented Apr 8, 2015 at 21:24

2 Answers 2

2

UPDATED

Use try and 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))

You could also leverage the atexit module to execute a function when the script exits.

import atexit

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

# your code here...

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

Sign up to request clarification or add additional context in comments.

Comments

0

You should likely take a look at Python Signal Handlers (specifically signal.CTRL_C_EVENT).

Here is a more-complete answer... NOTE: You can make your processes fairly unkillable by using such things.

#!/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()

2 Comments

I edited the question. I was using something similar but does this catch all exiting? such as ctrl + c ?
You should try running the script and test to see what happens when you send different signals... it's just an indefinite loop that prints the time once a second - and calls the handler for pretty much any signal it receives. In the case of UN*X, for example, a CTRL-C sends signal 2 (signal.SIGINT). In the case of your rephrased question, you likely just want to set SIGINT to be handled by a function like "stop" (which should be a def, not an if, which accepts two arguments).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.