Created
April 5, 2017 08:21
-
-
Save igreenfield/d5119d9e01133dbda4fe33f03508311b to your computer and use it in GitHub Desktop.
Kill linux processes running more then X time
This file contains hidden or 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
#!/usr/bin/env python | |
import psutil, time | |
max_time_from_start = 86400 | |
parent_pid = 1 | |
process_name = 'java' | |
string_in_cmd_args = 'workspace' | |
for process in psutil.process_iter(): | |
try: | |
if process.name() == process_name and process.ppid() == parent_pid: | |
if any([string_in_cmd_args in arg for arg in process.cmdline()]): | |
running_time = time.time() - process.create_time() | |
if running_time > max_time_from_start: | |
print "going to kill " + str(process) | |
process.send_signal(9) | |
except NoSuchProcess: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment