Created
September 19, 2017 14:08
-
-
Save costastf/ef839efd4f63b93845951da459658b39 to your computer and use it in GitHub Desktop.
Basic signal catching with python
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
#!/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