Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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