【问题标题】:how to get the list of process如何获取进程列表
【发布时间】:2012-10-23 18:58:20
【问题描述】:

我正在玩节点,只是将它安装在我的机器上。现在我想获取在我的机器上运行的进程列表,以便查看 Apache 是否正在运行、MySQL 是否已启动等?我怎样才能做到这一点?我的 js 文件中只有非常基本的代码。我什至不知道从哪里开始。

这是我的代码:

var http = require('http');
http.createServer(function(request, response){
    response.writeHead(200);
    response.write("Hello world");
    console.log('Listenning on port 1339');
    response.end();
}).listen(8080);

【问题讨论】:

标签: node.js


【解决方案1】:

好像没有直接的方法

但下面的视频可能会有所帮助。

【讨论】:

    【解决方案2】:

    据我所知,目前还没有一个模块可以跨平台。您可以使用子进程 API 来启动提供所需数据的工具。对于 Windows,只需启动内置的任务列表进程。

    var exec = require('child_process').exec;
    exec('tasklist', function(err, stdout, stderr) {
      // stdout is a string containing the output of the command.
      // parse it and look for the apache and mysql processes.
    });
    

    【讨论】:

    • 我真的很喜欢这种方法,它比我尝试过的其他节点模块更快并且使用更少的内存。你知道mac/linux相当于tasklist吗?
    • @d_scalzi 是的,在 Linux 中相当于 ps -aux 或使用 grep 来查找某个程序.... ps -aux | grep 字符串
    • 这是一个独立于平台的模块,使用命令行工具很愚蠢 - 请在此处查看其他答案
    【解决方案3】:

    ps-node

    获取节点中的进程列表:

    var ps = require('ps-node');
    
    ps.lookup({
    command: 'node',
    arguments: '--debug',
    }, function(err, resultList ) {
    if (err) {
        throw new Error( err );
    }
    
    resultList.forEach(function( process ){
        if( process ){
    
            console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
            }
        });
    });
    

    【讨论】:

    • 仅供参考:参数函数查找以这些参数启动的进程。例如,“node myScript.js --debug”
    • 本页选择的答案解释了ps背后的基本概念。添加对其他操作系统的支持看起来相对容易。 github.com/neekey/ps/blob/master/lib/index.js
    • 我只想指出 ps-node 是不可靠的,永远不应该在生产或您严重依赖它的地方使用。为什么?它在解析进程列表表时存在已知问题。这是不可预测的(即它取决于正在运行的进程)并且不止一次地让我陷入困境。您已收到警告。
    【解决方案4】:

    类 unix 系统的解决方案:

    const child_process = require('child_process');
    
    const displayProcessBy = (pattern) => {
        let command = `ps -aux | grep ${pattern}`;
        child_process.exec(command, (err, stdout, stdin) => {
            if (err) throw err;
            console.log(stdout);
        });
    }
    

    示例用法和结果

    displayProcessBy("nodejs");

    setivol+  7912  0.0  0.0  12732  2108 ?        S    10:56   0:00 grep nodejs
    setivol+ 12427  0.0  0.0 669552   712 pts/3    Tl   Dec16   0:00 nodejs
    setivol+ 14400  0.0  0.0 669552   644 pts/2    Tl   Dec15   0:00 nodejs
    setivol+ 14412  0.0  0.0 670576   224 pts/3    Tl   Dec16   0:00 nodejs
    setivol+ 14567  0.0  0.0 669552   436 pts/3    Tl   Dec15   0:00 nodejs
    setivol+ 14911  0.0  0.0 669552     0 pts/3    Tl   Dec15   0:00 nodejs
    setivol+ 15489  0.0  0.0 669552   712 pts/3    Tl   Dec16   0:00 nodejs
    setivol+ 15659  0.0  0.0 669520     0 pts/3    Tl   Dec16   0:00 nodejs --harmony
    setivol+ 16469  0.0  0.0 669520   704 pts/3    Tl   Dec16   0:00 nodejs --harmony
    setivol+ 20514  0.0  0.0 669552   664 pts/2    Tl   Dec15   0:00 nodejs
    

    displayProcessBy("python2")

    setivol+  8012  0.0  0.0   4336   712 ?        S    10:58   0:00 /bin/sh -c ps -aux | grep python2
    setivol+  8014  0.0  0.0  12728  2240 ?        S    10:58   0:00 grep python2
    

    测试环境

    $ uname -a
    Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux   
    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Debian
    Description:    Debian GNU/Linux 8.6 (jessie)
    Release:    8.6
    Codename:   jessie
    

    【讨论】:

      【解决方案5】:

      您还可以使用列出所有进程的 current-processes。 https://www.npmjs.com/package/current-processes

      结果包括进程使用的namepidcpumemory。 您还可以对结果进行排序并限制进程数。 结果如下所示:

       [ Process {
      pid: 31834,
      name: 'atom',
      cpu: 84,
      mem: { private: 19942400, virtual: 2048, usage: 0.4810941724241514 } }]
      

      【讨论】:

        【解决方案6】:

        ps-list 是一个更好的节点包,它也适用于 Linux、BSD 和 Windows 平台。

        const psList = require('ps-list');
        
        psList().then(data => {
            console.log(data);
            //=> [{pid: 3213, name: 'node', cmd: 'node test.js', cpu: '0.1'}, ...] 
        });
        

        【讨论】:

        • 虽然不支持一堆字段 - 只支持基础知识。
        • 您可能还想过滤掉name === 'fastlist...' 进程,该进程是为此插件收集数据的进程。 (win10)。
        猜你喜欢
        • 2020-08-15
        • 2013-06-24
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多