add 'kill' method

This commit is contained in:
PavelMuhortov 2021-06-20 19:23:02 +03:00
parent 9ead239c9f
commit 7a136f236f
2 changed files with 23 additions and 6 deletions

View File

@ -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)
```

View File

@ -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()))