ismaxim/terminator

此软件包已被弃用,不再维护。作者建议使用https://github.com/MaximGrynykha/winkill软件包代替。

在PHP中扫描并杀死Windows进程

v3.0.22 2024-05-18 18:22 UTC

README

Build Status

Winkill

将进程作为对象进行操作。通过属性查找特定的进程并结束它们。

⚙️ 安装

要在终端中安装此库,请运行以下命令

composer require maximgrynykha/winkill

用法

<?php

use Winkill\Winkill;

require_once 'vendor/autoload.php';

try {
    $winkill = new Winkill();
    $processes = $winkill->scan();
    // Get scanned processes
    $scanned = $processes->get();
 
    dd($scanned);
    // Select specific process(es)
    $selected = $processes->where(
        attribute: 'process_name',
        compareAs: '=',
        value: 'phpstorm64.exe'
    )->get();

    dd($selected);
    // Kill specific process(es)
    $killed = $processes->where(
        attribute: 'process_id',
        compareAs: '=',
        value: 11492
    )->kill();  

    // Note: If the processes have been killed or not found 
    // by the where-condition then returns an empty array.
    dd($killed);
} catch (\Winkill\Kernel\Interface\Exception|\Throwable $throwable) {
    die($throwable->getMessage());
}

API(基于'tasklist'命令)

属性名称 属性值 示例 比较运算符
process_name [字符串]: 带扩展名的名称 chrome.exe [字符串]: =, !=
[字符串]: 名称 chrome [字符串]: =, !=
[字符串]: 扩展名 .exe [字符串]: =, !=
process_id [整数]: ID号 [字符串]: =, !=
session_name [字符串]: 控制台/服务 [字符串]: =, !=
session_number [整数]: 范围为{0-1}的数字 [字符串]: =, !=
consumed_memory [整数]: 以Kb(千字节)为单位的数字 [字符串]: >, <, =, >=, <=, !=

选择进程

$processes->where('process_name', '=', 'chrome')->get();

$processes->where('process_id', '=', 11455)->get();

$processes->where('session_name', '=', 'console')->get();

$processes->where('session_number', '=', 1)->get();

// ⚠️ Note: consumed memory is estimated in Kb(kilobytes)
$processes->where('consumed_memory', '=', 128920)->get(); 

终止进程

$processes->where('process_name', '=', 'chrome')->kill();

$processes->where('process_id', '=', 11455)->kill();

//❗Alert: killing process(es) by attribute [session_name]
// may break you 🤯 and/or your computer 💥. Use it only 
// if you are 100% confident at the ending result.
$processes->where('session_name', '=', 'console')->kill();

//❗Alert: killing process(es) by attribute [session_number] 
// is the same danger as was said previously about attribute 
// [session_name], so be warned about using it at your risk.
$processes->where('session_number', '=', 1)->kill();

// ⚠️ Note: consumed memory is estimated in Kb(kilobytes)
$processes->where('consumed_memory', '=', 128920)->kill(); 

🧱 片段

// Terminate processes by an array of process names (all names are an example)
array_walk($processes_names = ['chrome', 'firefox', 'slack'],
    static fn(string $process_name): Process => 
        $processes->where('process_name', '=', $process_name)->kill(),
);

// Terminate processes by an array of process ids (all ids are an example)
array_walk($processes_ids = [1000, 5595, 17820],
    static fn(string $process_id): Process => 
        $processes->where('process_id', '=', $process_id)->kill(),
);

// You can switch between array_walk and array_map for your needs.
// [Confusing in difference?](https://stackoverflow.com/a/3432266/11591375)
$processes = $processes->where('consumed_memory', '<', 1000)->get();

// Sort processes on consumed memory by ASC
usort($processes, static fn(Process $process, Process $_process): int =>
    $process->consumed_memory <=> $_process->consumed_memory
);

// Sort processes on consumed memory by DESC
usort($processes, static fn(Process $process, Process $_process): int =>
    $_process->consumed_memory <=> $process->consumed_memory
);

📝 脚注

项目使用DRYSOLID原则。以下列出了使用的设计模式

创建型 结构型 行为型 其他*
抽象工厂 外观模式 策略模式 组合根
工厂方法 代理 命令
建造者

🤝 贡献

如果您无法使用此库解决问题,请写出您的解决方案,如果您想帮助其他使用此库的开发者(或者如果您想在发布新版本后保持解决方案正常工作,这将在包管理器依赖项中),请创建一个pull-request。我将很高兴将您优秀的代码添加到库中!

🐞 在GitHub问题上报告您发现的任何错误或问题。

📃 许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件