From 7a136f236f37198acee62a0eee0d9b74c0ec1872 Mon Sep 17 00:00:00 2001 From: PavelMuhortov Date: Sun, 20 Jun 2021 19:23:02 +0300 Subject: [PATCH] add 'kill' method --- README.md | 6 ++++-- srchproc.py | 23 +++++++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9762751..56f13d6 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ____ ## ffmpeger.py **Description:** FFmpeg management from Python -**Dependencies:** Python 3 (tested version 3.9.5), installed or downloaded ffmpeg, srchproc.py in the same directory +**Dependencies:** Python 3 (tested version 3.9.5), installed or downloaded ffmpeg, [srchproc.py](https://git.hmp.today/pavel.muhortov/utils#srchproc-py) in the same directory | PARAMETERS | DESCRIPTION | DEFAULT| |-------------|-------------|--------| @@ -35,6 +35,7 @@ from ffmpeger import FFmpeg FFmpeg.run(src='null, anull', preset='240p', fps=10) ``` + ____ ## sendmail.py **Description:** Sending email from Python @@ -72,6 +73,7 @@ msg = Mail(smtp_user='user@gmail.com', smtp_pass='pass', mail_dest='addr1@gmail. log = msg.send() print(log) ``` + ____ ## srchproc.py **Description:** Find a running process from Python @@ -83,6 +85,7 @@ ____ |**[--find]**|find process pid, name or arguments|| |**[--exclude]**|exclude process pid, name or arguments|`None`| |**[--self]**|find a clones of self|`True`| +|**[--kill]**|kill the process with pid|| Example usage in terminal with Python for find all running processes: ```shell @@ -104,4 +107,3 @@ if processes: for process in processes: print(process) ``` - diff --git a/srchproc.py b/srchproc.py index 40fc9b2..1f3f3c6 100644 --- a/srchproc.py +++ b/srchproc.py @@ -58,12 +58,10 @@ class Proc: Find all running process :return: list of dictionaries with descriptions of found processes """ - if platform.startswith('linux'): + if platform.startswith('linux') or platform.startswith('darwin'): return cls._list_linux() elif platform.startswith('win32'): return cls._list_windows() - elif platform.startswith('darwin'): - return cls._list_linux() else: return None @@ -92,6 +90,18 @@ class Proc: else: return proc_found + @classmethod + def kill(cls, pid: int): + """ + Kill the process by means of the OS + :param pid: process ID + :return: None + """ + if platform.startswith('linux') or platform.startswith('darwin'): + Popen(['kill', '-s', 'SIGKILL', str(pid)]) + elif platform.startswith('win32'): + Popen(['taskkill', '/PID', str(pid), '/F']) + if __name__ == "__main__": from argparse import ArgumentParser @@ -107,8 +117,13 @@ if __name__ == "__main__": help='exclude process pid, name or arguments') args.add_argument('--self', action='store_true', required=False, help='find a clones of self') + args.add_argument('--kill', type=int, required=False, + help='kill the process with pid') args = vars(args.parse_args()) - if args['find']: + processes = None + if args['kill']: + Proc.kill(args['kill']) + elif args['find']: processes = Proc.search(args['find'], args['exclude']) elif args['self']: processes = Proc.search(' '.join(argv), str(getpid()))