2012-11-03 32 views
7

我正在玩节点,并将其安装在我的机器上。现在我想获得在我的机器上运行的进程列表,以便我可以看到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); 

感谢

+0

你在使用什么操作系统? – Srinivas

+1

窗口,但会很好,如果你有东西的Windows和Linux – Autolycus

+0

https://github.com/viatropos/parse-processes/blob/master/index.js –

回答

10

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

如何读取进程及其子进程? – YumYumYum

+0

我真的很喜欢这种方法,它比我尝试过的其他节点模块快得多,使用的内存也少。你知道相当于tasklist的mac/linux吗? –

8

ps-node

要获取节点的进程列表:

类Unix系统
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); 
     } 
    }); 
}); 
+1

参考:参数函数查找以这些参数开始的进程。例如,“node myScript.js --debug” –

+1

ps-node似乎无法跨平台工作 – CodeAndCats

+0

此页上选定的答案解释了ps背后的基本概念。增加对其他操作系统的支持看起来相对容易。 https://github.com/neekey/ps/blob/master/lib/index.js – Eddie

0

解决方案:

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 
1

您也可以使用目前的流程,其中列出所有的进程ES。 https://www.npmjs.com/package/current-processes

结果包括PIDCPU和方法中使用存储器。 您也可以对结果进行排序并限制进程的数量。 结果看起来是这样的:

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

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