Skip to content

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
if uid != getuid():
# owned by some other user
if '/usr/local/bin/gunicorn' not in command:
# does not appear to be a gunicorn process
pid = int(basename(procdir))
if process_ids:
pid = choice(process_ids)
print 'Killing', pid,
kill(pid, SIGTERM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.