how to get the list of process


Question

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.

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);
1
14
11/27/2018 6:12:26 PM

Accepted Answer

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.

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.
});
16
11/3/2012 10:39:02 PM

See ps-node

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon