# Some functions for killing (sub)processes + children with ctypes - works with "shell=True"
## pip install subprocesskiller
#### Tested against Windows 10 / Python 3.10 / Anaconda
### Python
```python
from subprocesskiller import get_pro_properties, subprocess_timeout, kill_process_children_parents, kill_subprocs, kill_pid
# Get a dictionary of properties for all running processes
processprops = get_pro_properties()
# Kill all child processes of PID 9348 whose parent process has the executable name "python.exe".
# Don't kill any processes with the name "conhost.exe".
kill_process_children_parents(pid=9348,max_parent_exe='python.exe',dontkill=('Caption', 'conhost.exe'))
# Execute "ping -t 8.8.8.8" in a subprocess with a timeout of 4 seconds.
# If the process doesn't complete within 4 seconds, kill it and all of its child processes.
res = subprocess_timeout(
"ping -t 8.8.8.8",
shell=True,
timeout=4,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
)
# You can read the output (stderr/stdout) from a killed process
while g := res.stdout.readline():
print(g)
# Execute "ping -t 8.8.8.8" in a subprocess.
# Wait for 2 seconds, then kill all running python subprocess and all of its child processes except conhost.exe
subprocess.Popen("ping -t 8.8.8.8", shell=True)
time.sleep(2)
kill_subprocs(dontkill=(("Caption", "conhost.exe"),))
# Kills a pid
kill_pid(pid=9348)
```
### Command line
This is an example of how to use the Python script from the command line to kill a specific process and all its child processes. In this case, the script is being used to kill a process with PID 7348 that has a parent process with the name "chrome.exe".
To run the script from the command line, navigate to the directory where the script is saved and enter the following command:
```python
.\python.exe .\__init__.py --pid 7348 --max_parent_exe chrome.exe
```
This command specifies the PID of the process to be killed with the --pid flag and the name of the parent process that the process should have with the --max_parent_exe flag. In this case, the parent process name is "chrome.exe".
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/subprocesskiller",
"name": "subprocesskiller",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "pid,kill,subprocess,children,parents",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/96/38/412f19af0881b79a6e8a3a7f7c1ddd9ac099cbafd59d2917d05aad32c3fa/subprocesskiller-0.11.tar.gz",
"platform": null,
"description": "# Some functions for killing (sub)processes + children with ctypes - works with \"shell=True\" \r\n\r\n## pip install subprocesskiller\r\n\r\n\r\n#### Tested against Windows 10 / Python 3.10 / Anaconda\r\n\r\n\r\n### Python\r\n\r\n```python\r\n\r\nfrom subprocesskiller import get_pro_properties, subprocess_timeout, kill_process_children_parents, kill_subprocs, kill_pid\r\n# Get a dictionary of properties for all running processes\r\nprocessprops = get_pro_properties()\r\n\r\n# Kill all child processes of PID 9348 whose parent process has the executable name \"python.exe\".\r\n# Don't kill any processes with the name \"conhost.exe\".\r\nkill_process_children_parents(pid=9348,max_parent_exe='python.exe',dontkill=('Caption', 'conhost.exe'))\r\n\r\n\r\n# Execute \"ping -t 8.8.8.8\" in a subprocess with a timeout of 4 seconds.\r\n# If the process doesn't complete within 4 seconds, kill it and all of its child processes.\r\nres = subprocess_timeout(\r\n \"ping -t 8.8.8.8\",\r\n shell=True,\r\n timeout=4,\r\n stdout=subprocess.PIPE,\r\n stderr=subprocess.PIPE,\r\n stdin=subprocess.PIPE,\r\n)\r\n\r\n# You can read the output (stderr/stdout) from a killed process\r\nwhile g := res.stdout.readline():\r\n print(g)\r\n\r\n# Execute \"ping -t 8.8.8.8\" in a subprocess.\r\n# Wait for 2 seconds, then kill all running python subprocess and all of its child processes except conhost.exe\r\nsubprocess.Popen(\"ping -t 8.8.8.8\", shell=True)\r\ntime.sleep(2)\r\nkill_subprocs(dontkill=((\"Caption\", \"conhost.exe\"),))\r\n\r\n\r\n# Kills a pid \r\nkill_pid(pid=9348)\r\n\r\n\r\n\r\n```\r\n### Command line\r\n\r\nThis is an example of how to use the Python script from the command line to kill a specific process and all its child processes. In this case, the script is being used to kill a process with PID 7348 that has a parent process with the name \"chrome.exe\".\r\n\r\nTo run the script from the command line, navigate to the directory where the script is saved and enter the following command:\r\n\r\n```python\r\n.\\python.exe .\\__init__.py --pid 7348 --max_parent_exe chrome.exe\r\n```\r\n\r\nThis command specifies the PID of the process to be killed with the --pid flag and the name of the parent process that the process should have with the --max_parent_exe flag. In this case, the parent process name is \"chrome.exe\".\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Some functions for killing (sub)processes + children with ctypes - works with shell=True",
"version": "0.11",
"split_keywords": [
"pid",
"kill",
"subprocess",
"children",
"parents"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c1a233d93d7718f7246c02da6cc38252dee1072e5b21a88e684b890a8e1e1222",
"md5": "3baf98a509dc5f50f76f290885e7f5f4",
"sha256": "f7b6f340ec0cd85d0c6eaaa3bd55f5cd84c863870c39a30ad9d7bd64775f7ffd"
},
"downloads": -1,
"filename": "subprocesskiller-0.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3baf98a509dc5f50f76f290885e7f5f4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 9841,
"upload_time": "2023-04-15T10:05:11",
"upload_time_iso_8601": "2023-04-15T10:05:11.783809Z",
"url": "https://files.pythonhosted.org/packages/c1/a2/33d93d7718f7246c02da6cc38252dee1072e5b21a88e684b890a8e1e1222/subprocesskiller-0.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9638412f19af0881b79a6e8a3a7f7c1ddd9ac099cbafd59d2917d05aad32c3fa",
"md5": "4336e75264fd9e0e85c37696b557fe24",
"sha256": "f8eb65d38f23d632444979c6fc3b1d77997e67f4dad1483a297688a812f19437"
},
"downloads": -1,
"filename": "subprocesskiller-0.11.tar.gz",
"has_sig": false,
"md5_digest": "4336e75264fd9e0e85c37696b557fe24",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7420,
"upload_time": "2023-04-15T10:05:13",
"upload_time_iso_8601": "2023-04-15T10:05:13.423241Z",
"url": "https://files.pythonhosted.org/packages/96/38/412f19af0881b79a6e8a3a7f7c1ddd9ac099cbafd59d2917d05aad32c3fa/subprocesskiller-0.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-15 10:05:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "subprocesskiller",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "subprocesskiller"
}