Nodejs killall: a npm for kill all processes

Nodejs killall: a npm for kill all processes

killall

a npm for kill all processes

npm version

以前每次都这样杀掉所有进程

ps -ef|grep pm2|awk '{print $2}'|xargs kill -9

写起来比较麻烦,于是写了killall这个cli工具,其实我只要输入进程名称就可以杀掉所有相关进程

安装

sudo npm install -g killall

用法

killall pm2
killall node

欢迎拍砖


7 回复

Nodejs killall: a npm for kill all processes

killall

一个用于杀死所有指定进程的 npm 包。

npm version

以前每次杀掉所有进程时,都需要执行以下命令:

ps -ef | grep pm2 | awk '{print $2}' | xargs kill -9

这看起来很繁琐。因此,我编写了一个名为 killall 的 CLI 工具,它允许你只需输入进程名即可杀死所有相关的进程。

安装

你可以通过以下命令全局安装 killall

sudo npm install -g killall

用法

使用方法非常简单,只需提供你要杀死的进程名即可:

killall pm2
killall node

示例代码

为了更好地理解 killall 的工作原理,我们可以看看它的核心代码。以下是一个简单的实现示例:

const { exec } = require('child_process');

function killAll(processName) {
    // 使用 ps 命令查找所有与 processName 相关的进程 ID
    const psCommand = `ps -ef | grep "${processName}" | grep -v "grep" | awk '{print $2}'`;
    
    // 执行 ps 命令并获取进程 ID 列表
    exec(psCommand, (error, stdout, stderr) => {
        if (error) {
            console.error(`Error executing ps command: ${stderr}`);
            return;
        }
        
        const pids = stdout.trim().split('\n');
        
        // 遍历进程 ID 列表并杀死每个进程
        pids.forEach(pid => {
            const killCommand = `kill -9 ${pid}`;
            exec(killCommand, (error, stdout, stderr) => {
                if (error) {
                    console.error(`Error killing process ${pid}: ${stderr}`);
                    return;
                }
                console.log(`Killed process ${pid}`);
            });
        });
    });
}

// 使用示例
killAll('pm2');

解释

  1. 引入 child_process 模块:我们使用 child_process 模块来执行系统命令。
  2. 定义 killAll 函数:该函数接受一个进程名作为参数。
  3. 构建 ps 命令:使用 ps 命令查找与给定进程名相关的所有进程 ID,并过滤掉包含 grep 的行。
  4. 执行 ps 命令:使用 exec 方法执行 ps 命令,并获取进程 ID 列表。
  5. 遍历进程 ID 并杀死进程:对于每个进程 ID,构建并执行 kill 命令以杀死进程。

希望这个简单的实现能帮助你更好地理解 killall 的工作原理。如果你有任何问题或建议,请随时提出!


node写cli真心很方便,几分钟就可以搞定

也可以直接调用pgrep或者pkill

其实系统内置了pgrep和pkill和killall命令的,此repo权当练习自娱自乐吧

  • 已测试mac 10.9.3
  • 已测试centOS 6.0
	~  man pgrep
	PGREP(1)                      Linux User’s Manual                     PGREP(1)
NAME
       pgrep, pkill - look up or signal processes based on name and other attributes


SYNOPSIS
       pgrep [-flvx] [-d delimiter] [-n|-o] [-P ppid,...] [-g pgrp,...]
            [-s sid,...] [-u euid,...] [-U uid,...] [-G gid,...]
            [-t term,...] [pattern]

       pkill [-signal] [-fvx] [-n|-o] [-P ppid,...] [-g pgrp,...]
            [-s sid,...] [-u euid,...] [-U uid,...] [-G gid,...]
            [-t term,...] [pattern]


DESCRIPTION
       pgrep  looks  through  the  currently running processes and lists the process IDs which matches the selection criteria to stdout.  All the criteria have to
       match.  For example,

       pgrep -u root sshd

       will only list the processes called sshd AND owned by root.  On the other hand,

       pgrep -u root,daemon

       will list the processes owned by root OR daemon.

       pkill will send the specified signal (by default SIGTERM) to each process instead of listing them on stdout.
~  man killall

NAME
       killall - kill processes by name

SYNOPSIS
       killall [-Z,--context pattern] [-e,--exact] [-g,--process-group] [-i,--interactive] [-q,--quiet] [-r,--regexp] [-s,--signal signal] [-u,--user user]
       [-v,--verbose] [-w,--wait] [-I,--ignore-case] [-V,--version] [--] name ...
       killall -l
       killall -V,--version

DESCRIPTION
       killall sends a signal to all processes running any of the specified commands. If no signal name is specified, SIGTERM is sent.

       Signals can be specified either by name (e.g. -HUP or -SIGHUP ) or by number (e.g. -1) or by option -s.

       If the command name is not regular expression (option -r) and contains a slash (/), processes executing that particular file will be selected for  killing,
       independent of their name.

       killall  returns  a  zero  return code if at least one process has been killed for each listed command, or no commands were listed and at least one process
       matched the -u and -Z search criteria. killall returns non-zero otherwise.

       A killall process never kills itself (but may kill other killall processes).

早在2000年,就有人再sun的系统里实现了pgrep等功能,然后迁移到gnu。。。。。于是。。。。。

$ npm install  -g fuck-you

我一直用这个。

killall 是一个通过 npm 安装的命令行工具,用于根据进程名来杀死所有匹配的进程。这个工具简化了使用复杂的 shell 命令来查找并杀死进程的操作。

安装

你可以通过以下命令全局安装 killall

sudo npm install -g killall

使用方法

安装完成后,你可以使用 killall 命令加上进程名来杀死所有的相关进程。例如:

killall pm2
killall node

这两个命令将会分别杀死所有名为 pm2node 的进程。

示例代码

killall 并不是一个用于 Node.js 脚本内部使用的库,而是一个独立的 CLI 工具。因此,你不会直接在 Node.js 代码中调用它。但是,如果你希望在 Node.js 中实现类似的功能,可以参考下面的代码:

const { exec } = require('child_process');

function killProcessesByName(processName) {
    exec(`pkill -f ${processName}`, (error, stdout, stderr) => {
        if (error) {
            console.error(`执行错误: ${stderr}`);
            return;
        }
        console.log(`进程已杀: ${stdout}`);
    });
}

// 杀死所有名为 'pm2' 的进程
killProcessesByName('pm2');

// 杀死所有名为 'node' 的进程
killProcessesByName('node');

这段代码利用 Node.js 的 child_process 模块中的 exec 函数来执行系统命令,从而达到杀死指定进程的目的。注意这里的 pkill -f 命令可以根据进程名(或部分进程名)杀死进程。

回到顶部