Created
September 12, 2018 23:24
-
-
Save onjin/b723d6550394cb585f0398688fecd8f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Start interactive console on SIGUSR1 signal. | |
run: | |
$ python ./sigusr1_debug.py | |
and in another window | |
$ ps aux|grep sigusr1_debug | |
$ kill -10 [process pid] | |
and in first window you get: | |
. | |
Signal received : entering python shell. | |
Traceback: | |
File "sigusr1_debug.py", line 27, in <module> | |
time.sleep(1) | |
>>> | |
""" | |
import code | |
import signal | |
import time | |
import traceback | |
def debug(sig, frame): | |
"""Interrupt running process, and provide a python prompt for | |
interactive debugging.""" | |
d = {'_frame': frame} # Allow access to frame object. | |
d.update(frame.f_globals) # Unless shadowed by global | |
d.update(frame.f_locals) | |
i = code.InteractiveConsole(d) | |
message = "Signal received : entering python shell.\nTraceback:\n" | |
message += ''.join(traceback.format_stack(frame)) | |
i.interact(message) | |
def listen(): | |
signal.signal(signal.SIGUSR1, debug) # Register handler | |
listen() | |
while 1: | |
time.sleep(1) | |
print('.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment