Skip to content

Instantly share code, notes, and snippets.

@costastf
Created September 19, 2017 14:08
Show Gist options
  • Save costastf/ef839efd4f63b93845951da459658b39 to your computer and use it in GitHub Desktop.
Save costastf/ef839efd4f63b93845951da459658b39 to your computer and use it in GitHub Desktop.
Basic signal catching with python
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import signal
import logging
__author__ = 'Costas Tyfoxylos <costas.tyf@gmail.com>'
__docformat__ = 'plaintext'
logger = logging.getLogger('myapp')
logging.basicConfig(level=logging.DEBUG)
def receive_signal(signum, stack):
logger.debug('Caught signal {signal}, ignoring.'.format(signal=signum))
def main():
uncatchable = ['SIG_DFL', 'SIGSTOP', 'SIGKILL']
for caught in [sig for sig in dir(signal) if sig.startswith("SIG")
and sig not in uncatchable]:
signum = getattr(signal, caught)
signal.signal(signum, receive_signal)
while True:
raw_input('You can kill -"Some signal number" $PID to see the output')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment