Skip to main content

A library for killing processes/subprocesses the most gracefully way possible

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

procciao-0.13.tar.gz (60.2 kB view details)

Uploaded Source

Built Distribution

procciao-0.13-py3-none-any.whl (60.4 kB view details)

Uploaded Python 3

File details

Details for the file procciao-0.13.tar.gz.

File metadata

  • Download URL: procciao-0.13.tar.gz
  • Upload date:
  • Size: 60.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for procciao-0.13.tar.gz
Algorithm Hash digest
SHA256 8890fa0765ebc02b483e590583e613c9345253672c000f1793f88bdd762da669
MD5 9494544dbac83eae1cd753e6db912d10
BLAKE2b-256 9c889d9fdb4eddffce5ccc383167f062329b10ff7985885d677afa77ae72d332

See more details on using hashes here.

File details

Details for the file procciao-0.13-py3-none-any.whl.

File metadata

  • Download URL: procciao-0.13-py3-none-any.whl
  • Upload date:
  • Size: 60.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for procciao-0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 f392c6413d5a025a2c2ef3b076308729ba2439a1b6d2a4a756a0688d9db2a5a3
MD5 c866bfcd32941553a1468af972594c1f
BLAKE2b-256 823412de9a4c61640222c36d22389374c50335af330e1aa436620d033272054d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page