如何获取进程列表

Aut*_*cus 14 node.js

我正在玩节点,只是将它安装在我的机器上.现在我想得到一个在我的机器上运行的进程列表,这样我就可以看到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);
Run Code Online (Sandbox Code Playgroud)

Bad*_*yon 16

据我所知,还没有一个模块可以做到这个跨平台.您可以使用子进程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.
});
Run Code Online (Sandbox Code Playgroud)


Edd*_*die 8

请参阅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 );
        }
    });
});
Run Code Online (Sandbox Code Playgroud)


KAR*_*ván 7

ps-list is a better node package for the job, it is working on Linux, BSD, and Windows platforms too.

const psList = require('ps-list');

psList().then(data => {
    console.log(data);
    //=> [{pid: 3213, name: 'node', cmd: 'node test.js', cpu: '0.1'}, ...] 
});
Run Code Online (Sandbox Code Playgroud)


sha*_*359 6

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

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

 [ Process {
pid: 31834,
name: 'atom',
cpu: 84,
mem: { private: 19942400, virtual: 2048, usage: 0.4810941724241514 } }]
Run Code Online (Sandbox Code Playgroud)