Skip to main content

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

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.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".

Metadata

Release files for subprocesskiller 0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for subprocesskiller 0.11
File Size Uploaded Provenance Actions
subprocesskiller-0.11.tar.gz 7.4 kB Without provenance Details for subprocesskiller-0.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for subprocesskiller 0.11
File Interpreter ABI Platform Actions
subprocesskiller-0.11-py3-none-any.whl Python 3 none any Details for subprocesskiller-0.11-py3-none-any.whl

Total release size: 17.3 kB

Release files / subprocesskiller-0.11.tar.gz

Download URL subprocesskiller-0.11.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
f8eb65d38f23d632444979c6fc3b1d77997e67f4dad1483a297688a812f19437
BLAKE2b-256 checksum
How to use checksums
9638412f19af0881b79a6e8a3a7f7c1ddd9ac099cbafd59d2917d05aad32c3fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / subprocesskiller-0.11-py3-none-any.whl

Download URL subprocesskiller-0.11-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7b6f340ec0cd85d0c6eaaa3bd55f5cd84c863870c39a30ad9d7bd64775f7ffd
BLAKE2b-256 checksum
How to use checksums
c1a233d93d7718f7246c02da6cc38252dee1072e5b21a88e684b890a8e1e1222
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for subprocesskiller 0.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.11 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page