Skip to content

Instantly share code, notes, and snippets.

@onjin
Created September 12, 2018 23:24
Show Gist options
  • Save onjin/b723d6550394cb585f0398688fecd8f9 to your computer and use it in GitHub Desktop.
Save onjin/b723d6550394cb585f0398688fecd8f9 to your computer and use it in GitHub Desktop.
"""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