Giter Site home page Giter Site logo

hansalemaos / procciao Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 126 KB

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

Home Page: https://pypi.org/project/procciao/

License: MIT License

Python 100.00%
gracefully kill process subprocess

procciao's Introduction

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

procciao's People

Contributors

hansalemaos avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.