如何获得进程列表

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

谢谢

据我所知,目前还没有一个模块可以做这个跨平台的。 您可以使用subprocessAPI来启动将提供所需数据的工具。 对于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. }); 

请参阅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 ); } }); }); 

似乎他们不是任何直接的方法

但下面的video可能会有帮助

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

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

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

针对类Unix系统的解决scheme:

 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 

testing环境

 $ 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 

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