Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created November 26, 2012 15:08
Show Gist options
  • Select an option

  • Save h2rd/4148690 to your computer and use it in GitHub Desktop.

Select an option

Save h2rd/4148690 to your computer and use it in GitHub Desktop.
terminate application
#!/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