# A library for killing processes/subprocesses the most gracefully way possible
## pip install procciao
### Tested against Windows 10 / Python 3.11 / Anaconda
The attempts to kill processes go from the most gracefully to the most forcefully
1) https://github.com/ElyDotDev/windows-kill SIGINT/SIGBREAK (DLL/EXE)
2) powershell SIGINT/SIGBREAK/CLOSE
3) taskkill
```PY
from procciao import kill_proc
from procciao.procdict import get_procs
import re
d1 = get_procs(
use_wmic=True,
columns_to_parse=(
# "CommandLine", -> if CommandLine is in columns_to_parse, all columns are parsed due to some strange output (header in more than one line)
"CreationClassName",
"CreationDate",
"CSCreationClassName",
"Description",
"ExecutablePath",
"ExecutionState",
"Handle",
"HandleCount",
"Name",
"ProcessId",
"ReadOperationCount",
"ReadTransferCount",
"ThreadCount",
"WriteOperationCount",
"WriteTransferCount",
),
searchdict={
"ExecutablePath": re.compile(
r".*chrome.exe.*"
), # has to be a compiled regex, if regex is desired
"HandleCount": lambda x: int(x) > 1, # use functions for numbers and convert the data dtypes
"Description": "chrome.exe", # compares with ==
},
)
d2 = get_procs(
use_wmic=False,
columns_to_parse=(
"HandleCount",
"Path",
"Company",
"CPU",
"ProductVersion",
"Description",
"Product",
"HasExited",
"ExitTime",
"Handle",
"MainWindowHandle",
"MainWindowTitle",
"MainModule",
"ProcessName",
"Responding",
"StartTime",
"SynchronizingObject",
"UserProcessorTime",
),
searchdict={
"Path": re.compile(
r".*opera.exe.*", flags=re.I
), # has to be a compiled regex, if regex is desired
"Handle": lambda x: int(x) > 10, # for numbers
},
)
for k, v in d1.items():
print(k, v)
print(v["get_children_tree"]()) # print children tree
for k, v in d1.items():
print(k, v)
v["kill"]()
for k, v in d2.items():
print(k, v)
print(v["get_children_flat"]()) # print children (flat)
for k, v in d2.items():
print(k, v)
v["kill"](
protect_myself=True, # to overwrite the config
winkill_sigint=False,
winkill_sigbreak=False,
winkill_sigint_dll=False,
winkill_sigbreak_dll=False,
powershell_sigint=False,
powershell_sigbreak=False,
powershell_close=False,
multi_children_kill=False,
multi_children_always_ignore_pids=(0, 4),
print_output=True,
taskkill_as_last_option=True,
)
# get all data from all procs without any kill / get children functions
d3 = get_procs(use_wmic=True, columns_to_parse=(), searchdict=None, add_functions=False)
d4 = get_procs(
use_wmic=False, columns_to_parse=(), searchdict=None, add_functions=False
)
# killing subprocesses
import subprocess
import time
p = subprocess.Popen("ping -n 30000 google.com", shell=False)
time.sleep(5)
kill_proc(p.pid)
p = subprocess.Popen(
"ping -n 30000 google.com",
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
time.sleep(5)
kill_proc(p.pid)
print(p.stdout.read())
print(p.stderr.read())
# works even with shell=True
p = subprocess.Popen(
"dir /b/s",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
)
time.sleep(5)
# goes from the most gracefully to the most forcefully
# 1) https://github.com/ElyDotDev/windows-kill
# 2) powershell
# 3) taskkill
# Exact order:
kill_proc(
pid=p.pid,
kill_timeout=5,
protect_myself=True, # important, protect_myselfis False, you might kill the whole python process you are in.
winkill_sigint_dll=True, # dll first
winkill_sigbreak_dll=True,
winkill_sigint=True, # exe from outside
winkill_sigbreak=True,
powershell_sigint=True,
powershell_sigbreak=True,
powershell_close=True,
multi_children_kill=True, # try to kill each child one by one
multi_children_always_ignore_pids=(0, 4), # ignore system processes
print_output=True,
taskkill_as_last_option=True, # this always works, but it is not gracefully anymore
)
print(p.stdout.read())
print(p.stderr.read())
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/procciao",
"name": "procciao",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "killing, pid",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9c/88/9d9fdb4eddffce5ccc383167f062329b10ff7985885d677afa77ae72d332/procciao-0.13.tar.gz",
"platform": null,
"description": "\r\n# A library for killing processes/subprocesses the most gracefully way possible\r\n\r\n## pip install procciao\r\n\r\n### Tested against Windows 10 / Python 3.11 / Anaconda\r\n\r\n\r\nThe attempts to kill processes go from the most gracefully to the most forcefully\r\n\r\n1) https://github.com/ElyDotDev/windows-kill SIGINT/SIGBREAK (DLL/EXE)\r\n2) powershell SIGINT/SIGBREAK/CLOSE\r\n3) taskkill\r\n\r\n\r\n\r\n```PY\r\nfrom procciao import kill_proc\r\nfrom procciao.procdict import get_procs\r\nimport re\r\n\r\nd1 = get_procs(\r\n use_wmic=True,\r\n columns_to_parse=(\r\n # \"CommandLine\", -> if CommandLine is in columns_to_parse, all columns are parsed due to some strange output (header in more than one line)\r\n \"CreationClassName\",\r\n \"CreationDate\",\r\n \"CSCreationClassName\",\r\n \"Description\",\r\n \"ExecutablePath\",\r\n \"ExecutionState\",\r\n \"Handle\",\r\n \"HandleCount\",\r\n \"Name\",\r\n \"ProcessId\",\r\n \"ReadOperationCount\",\r\n \"ReadTransferCount\",\r\n \"ThreadCount\",\r\n \"WriteOperationCount\",\r\n \"WriteTransferCount\",\r\n ),\r\n searchdict={\r\n \"ExecutablePath\": re.compile(\r\n r\".*chrome.exe.*\"\r\n ), # has to be a compiled regex, if regex is desired\r\n \"HandleCount\": lambda x: int(x) > 1, # use functions for numbers and convert the data dtypes\r\n \"Description\": \"chrome.exe\", # compares with ==\r\n },\r\n)\r\nd2 = get_procs(\r\n use_wmic=False,\r\n columns_to_parse=(\r\n \"HandleCount\",\r\n \"Path\",\r\n \"Company\",\r\n \"CPU\",\r\n \"ProductVersion\",\r\n \"Description\",\r\n \"Product\",\r\n \"HasExited\",\r\n \"ExitTime\",\r\n \"Handle\",\r\n \"MainWindowHandle\",\r\n \"MainWindowTitle\",\r\n \"MainModule\",\r\n \"ProcessName\",\r\n \"Responding\",\r\n \"StartTime\",\r\n \"SynchronizingObject\",\r\n \"UserProcessorTime\",\r\n ),\r\n searchdict={\r\n \"Path\": re.compile(\r\n r\".*opera.exe.*\", flags=re.I\r\n ), # has to be a compiled regex, if regex is desired\r\n \"Handle\": lambda x: int(x) > 10, # for numbers\r\n },\r\n)\r\n\r\nfor k, v in d1.items():\r\n print(k, v)\r\n print(v[\"get_children_tree\"]()) # print children tree\r\nfor k, v in d1.items():\r\n print(k, v)\r\n v[\"kill\"]()\r\n\r\nfor k, v in d2.items():\r\n print(k, v)\r\n print(v[\"get_children_flat\"]()) # print children (flat)\r\nfor k, v in d2.items():\r\n print(k, v)\r\n v[\"kill\"](\r\n protect_myself=True, # to overwrite the config\r\n winkill_sigint=False,\r\n winkill_sigbreak=False,\r\n winkill_sigint_dll=False,\r\n winkill_sigbreak_dll=False,\r\n powershell_sigint=False,\r\n powershell_sigbreak=False,\r\n powershell_close=False,\r\n multi_children_kill=False,\r\n multi_children_always_ignore_pids=(0, 4),\r\n print_output=True,\r\n taskkill_as_last_option=True,\r\n )\r\n\r\n# get all data from all procs without any kill / get children functions\r\nd3 = get_procs(use_wmic=True, columns_to_parse=(), searchdict=None, add_functions=False)\r\nd4 = get_procs(\r\n use_wmic=False, columns_to_parse=(), searchdict=None, add_functions=False\r\n)\r\n\r\n# killing subprocesses\r\nimport subprocess\r\nimport time\r\n\r\np = subprocess.Popen(\"ping -n 30000 google.com\", shell=False)\r\ntime.sleep(5)\r\nkill_proc(p.pid)\r\n\r\np = subprocess.Popen(\r\n \"ping -n 30000 google.com\",\r\n shell=False,\r\n stdout=subprocess.PIPE,\r\n stderr=subprocess.PIPE,\r\n)\r\ntime.sleep(5)\r\nkill_proc(p.pid)\r\nprint(p.stdout.read())\r\nprint(p.stderr.read())\r\n\r\n# works even with shell=True\r\np = subprocess.Popen(\r\n \"dir /b/s\",\r\n stdout=subprocess.PIPE,\r\n stderr=subprocess.PIPE,\r\n shell=True,\r\n)\r\ntime.sleep(5)\r\n\r\n# goes from the most gracefully to the most forcefully\r\n# 1) https://github.com/ElyDotDev/windows-kill\r\n# 2) powershell\r\n# 3) taskkill\r\n# Exact order:\r\nkill_proc(\r\n pid=p.pid,\r\n kill_timeout=5,\r\n protect_myself=True, # important, protect_myselfis False, you might kill the whole python process you are in.\r\n winkill_sigint_dll=True, # dll first\r\n winkill_sigbreak_dll=True,\r\n winkill_sigint=True, # exe from outside\r\n winkill_sigbreak=True,\r\n powershell_sigint=True,\r\n powershell_sigbreak=True,\r\n powershell_close=True,\r\n multi_children_kill=True, # try to kill each child one by one\r\n multi_children_always_ignore_pids=(0, 4), # ignore system processes\r\n print_output=True,\r\n taskkill_as_last_option=True, # this always works, but it is not gracefully anymore\r\n)\r\n\r\nprint(p.stdout.read())\r\nprint(p.stderr.read())\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A library for killing processes/subprocesses the most gracefully way possible",
"version": "0.13",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/procciao"
},
"split_keywords": [
"killing",
" pid"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "823412de9a4c61640222c36d22389374c50335af330e1aa436620d033272054d",
"md5": "c866bfcd32941553a1468af972594c1f",
"sha256": "f392c6413d5a025a2c2ef3b076308729ba2439a1b6d2a4a756a0688d9db2a5a3"
},
"downloads": -1,
"filename": "procciao-0.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c866bfcd32941553a1468af972594c1f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 60363,
"upload_time": "2024-04-19T11:08:44",
"upload_time_iso_8601": "2024-04-19T11:08:44.097011Z",
"url": "https://files.pythonhosted.org/packages/82/34/12de9a4c61640222c36d22389374c50335af330e1aa436620d033272054d/procciao-0.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9c889d9fdb4eddffce5ccc383167f062329b10ff7985885d677afa77ae72d332",
"md5": "9494544dbac83eae1cd753e6db912d10",
"sha256": "8890fa0765ebc02b483e590583e613c9345253672c000f1793f88bdd762da669"
},
"downloads": -1,
"filename": "procciao-0.13.tar.gz",
"has_sig": false,
"md5_digest": "9494544dbac83eae1cd753e6db912d10",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 60164,
"upload_time": "2024-04-19T11:08:46",
"upload_time_iso_8601": "2024-04-19T11:08:46.884566Z",
"url": "https://files.pythonhosted.org/packages/9c/88/9d9fdb4eddffce5ccc383167f062329b10ff7985885d677afa77ae72d332/procciao-0.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-19 11:08:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "procciao",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "procciao"
}