Created
January 9, 2022 20:30
-
-
Save wtbarnes/da437c6029638bd2fc7735335e2dac41 to your computer and use it in GitHub Desktop.
Python script for grepping and killing particularly processes
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
import subprocess | |
import click | |
@click.command() | |
@click.option('--filters', type=str, default=[], multiple=True) | |
@click.option('--dry-run', is_flag=True, default=False, help='If set, do not kill anything.') | |
def cli(filters, dry_run): | |
cmd = 'ps aux' | |
for f in filters: | |
cmd += f' | grep {f}' | |
cmd += '| grep -v grep' # this excludes the grep process | |
cmd += '| grep -v kill_processes.py' # this excludes our script | |
output = subprocess.run([cmd], shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) | |
proc = output.stdout.decode('utf-8').strip().split('\n') | |
for p in proc: | |
print(p) | |
pid = p.split()[1] | |
if not dry_run: | |
subprocess.call(['kill', '-9', pid]) | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment