Created
November 26, 2012 15:08
-
-
Save h2rd/4148690 to your computer and use it in GitHub Desktop.
terminate application
This file contains hidden or 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/python | |
| # -*- coding: utf-8 -*- | |
| import os | |
| import sys | |
| import time | |
| import signal | |
| def termination_handler(signum, frame): | |
| global running | |
| global pidfile | |
| print 'You have requested to terminate the application...' | |
| sys.stdout.flush() | |
| running = 0 | |
| os.unlink(pidfile) | |
| running = 1 | |
| signal.signal(signal.SIGINT,termination_handler) | |
| pid = str(os.getpid()) | |
| pidfile = '/tmp/' + os.path.basename(__file__).split('.')[0] + '.pid' | |
| if os.path.isfile(pidfile): | |
| print "%s already exists, exiting" % pidfile | |
| sys.exit() | |
| else: | |
| file(pidfile, 'w').write(pid) | |
| # Do some actual work here | |
| while running: | |
| time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment