-
-
Save x-Code-x/5449067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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