Skip to main content

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

Metadata

Release files for procciao 0.13

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

Source distribution (sdist)

Source distribution for procciao 0.13
File Size Uploaded Provenance Actions
procciao-0.13.tar.gz 60.2 kB Without provenance Details for procciao-0.13.tar.gz

Built distribution (wheel)

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

Total release size: 120.5 kB

Release files / procciao-0.13.tar.gz

Download URL procciao-0.13.tar.gz
Size 60.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8890fa0765ebc02b483e590583e613c9345253672c000f1793f88bdd762da669
BLAKE2b-256 checksum
How to use checksums
9c889d9fdb4eddffce5ccc383167f062329b10ff7985885d677afa77ae72d332
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / procciao-0.13-py3-none-any.whl

Download URL procciao-0.13-py3-none-any.whl
Size 60.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f392c6413d5a025a2c2ef3b076308729ba2439a1b6d2a4a756a0688d9db2a5a3
BLAKE2b-256 checksum
How to use checksums
823412de9a4c61640222c36d22389374c50335af330e1aa436620d033272054d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Security for procciao 0.13

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.

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