node.js 如何获取进程列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13206724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:35:32  来源:igfitidea点击:

how to get the list of process

node.js

提问by Asim Zaidi

I am playing around with node and just installed it on my machine. Now I want to get a list of processes running on my machine so I can see whether Apache is running, MySQL is started, etc? How can I do that? I just have very basic code in my js file. I don't even know where to begin on this.

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

Here is my code:

这是我的代码:

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);

回答by BadCanyon

As far as I know there isn't a module (yet) to do this cross-platform. You can use the child process API to launch tools that will give the data you want. For Windows, just launch the built-in tasklist process.

据我所知,还没有一个模块来做这个跨平台的。您可以使用子进程 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.
});

回答by Eddie

See ps-node

参见ps 节点

To get a list of processes in 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 );
        }
    });
});

回答by shadow0359

You can also use current-processes which lists all the processes. https://www.npmjs.com/package/current-processes

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

The result includes name,pid,cpuand memoryused by process. You can also sort the result and limit the number of processes. The result looks like this:

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

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

回答by KARASZI István

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

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'}, ...] 
});

回答by PADYMKO

Solution for unix-like systems:

类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);
    });
}

Examples usage and results

示例用法和结果

displayProcessBy("nodejs");

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")

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

Testing environment

测试环境

$ 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

回答by Hitesh Chavda

Seems there isn't any direct methods

似乎没有任何直接的方法

but below videos might help.

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