Skip to content

Instantly share code, notes, and snippets.

@x-Code-x
Forked from migurski/gunicorn-slayer.py
Created April 24, 2013 02:09
Show Gist options
  • Save x-Code-x/5449067 to your computer and use it in GitHub Desktop.
Save x-Code-x/5449067 to your computer and use it in GitHub Desktop.
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