Last active
December 9, 2024 17:30
-
-
Save tmr232/a65a96483ecfc2eaf37028e0f0201b8a to your computer and use it in GitHub Desktop.
Utility script to kill jcef_helper processes (because IntelliJ likes leaking them during debugging)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# dependencies = [ | |
# "psutil>=6.1.0", | |
# "rich>=13.9.4", | |
# "structlog>=24.4.0", | |
# "typer>=0.15.1", | |
# ] | |
# /// | |
import time | |
from typing import Iterator | |
import rich.progress | |
import structlog | |
import typer | |
import psutil | |
JCEF_NAME = "jcef_helper.exe" | |
INTERVAL = 10 # 10 seconds | |
app = typer.Typer() | |
def find_processes(**attrs)->Iterator[psutil.Process]: | |
for process in psutil.process_iter(list(attrs.keys())): | |
if process.info == attrs: | |
yield process | |
def find_orphaned_processes(**attrs)->Iterator[psutil.Process]: | |
return (proc for proc in find_processes(**attrs) if not proc.parent()) | |
def kill_orphaned_processes(**attrs)->int: | |
if not attrs: | |
raise ValueError("Must provide process attributes for killing processes!") | |
procs_to_kill = list(find_orphaned_processes(**attrs)) | |
for process in procs_to_kill: | |
process.kill() | |
return len(procs_to_kill) | |
log =structlog.get_logger() | |
@app.command() | |
def main(): | |
total_kills = 0 | |
with rich.progress.Progress() as progress: | |
progress.add_task(f"Hunting for {JCEF_NAME}", total=None) | |
while 1: | |
kill_count = kill_orphaned_processes(name=JCEF_NAME) | |
total_kills += kill_count | |
message = None | |
if kill_count: | |
message = f"{kill_count} instances of {JCEF_NAME} killed." | |
log.info(message,total_kills=total_kills) | |
time.sleep(INTERVAL) | |
if __name__ == "__main__": | |
app() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment