Skip to content

Instantly share code, notes, and snippets.

@migurski
Created June 21, 2012 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save migurski/2964260 to your computer and use it in GitHub Desktop.
Save migurski/2964260 to your computer and use it in GitHub Desktop.
Gunicorn Slayer
from sys import argv
from time import time
from glob import glob
from os import stat, kill, getuid
from os.path import basename, dirname, join
from datetime import datetime
from random import choice
from signal import SIGTERM
if __name__ == '__main__':
min_age = int(argv[1])
process_ids = []
for proccmd in glob('/proc/*/cmdline'):
procdir = dirname(proccmd)
command = open(proccmd).read().split('\x00')
age = time() - stat(procdir).st_ctime
uid = stat(procdir).st_uid
if age < min_age:
# not old enough
continue
if uid != getuid():
# owned by some other user
continue
if '/usr/local/bin/gunicorn' not in command:
# does not appear to be a gunicorn process
continue
pid = int(basename(procdir))
process_ids.append(pid)
if process_ids:
pid = choice(process_ids)
print 'Killing', pid, datetime.now()
kill(pid, SIGTERM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment