Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/codylane/python_signalhandler
Cool little library for trapping and handling signals and performing a custom action
https://github.com/codylane/python_signalhandler
Last synced: 4 days ago
JSON representation
Cool little library for trapping and handling signals and performing a custom action
- Host: GitHub
- URL: https://github.com/codylane/python_signalhandler
- Owner: codylane
- Created: 2014-01-25T18:20:48.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-02-01T20:28:21.000Z (almost 11 years ago)
- Last Synced: 2023-08-03T01:12:22.652Z (over 1 year ago)
- Language: Python
- Size: 172 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
python-signalhandler
====================Cool little library for trapping and handling signals and performing a custom action. This code was created back in 2009 when I had a need to write a custom daemon in python. The original daemon code was found here, http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/ and this signal handler code was used to processing IPC signals to inform the daemon code to perform special actions.
Example Code sighandler.py
==========================
```
import signal
import os
import sys
from SignalHandler import SignalHandlerdef stop():
print "Called the stop().... Exitting 0"
sys.exit(0)def abort():
print "Called abort().... Exitting 1"
sys.exit(1)if __name__ == '__main__':
# Create SignalHandler object
sighandler = SignalHandler()# Now register a SIGTERM (kill -15) and callback
# object when signal is trapped.
sighandler.register( signal.SIGTERM, stop)# Register a SIGINT (kill -2) and callback
# object when sign is trapped.
# This is equivalent to CTRL-C
sighandler.register( signal.SIGINT, abort )# Display the current registered signal events.
# This is just for show and tell purposes.
print "Implemented Signals: ", sighandler.getActions()while True:
print "Waiting for signal.... Running pid: %s" %(os.getpid())
signal.pause()
```
Example Usage From Code Above
=============================
```
$ ./sighandler.py &
[1] 5277
Implemented Signals: (, )
Waiting for signal.... Running pid: 5277$ kill 5277
Called the stop().... Exitting 0
$
```