20

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

7 Answers 7

25

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.
});
4
  • I really like this method, it's much faster and uses less memory than other node modules I've tried. Do you know the mac/linux equivalent to tasklist?
    – d_scalzi
    Commented Dec 27, 2017 at 17:52
  • @d_scalzi yes, in Linux the equivalent is ps -aux or with grep to find a certain program.... ps -aux | grep string Commented Apr 11, 2018 at 21:52
  • the is a platform independent module for this, it's silly to use a command line tool - see other answers here
    – Andris
    Commented Sep 10, 2020 at 12:02
  • This is giving me whole table in the form of string. How can query in that? Commented Jun 8, 2023 at 14:44
11

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'}, ...] 
});
2
  • Doesn't support a bunch of fields though - only the basics.
    – vaughan
    Commented Aug 5, 2019 at 22:32
  • 1
    You may also want to filter out the name === 'fastlist...' process which is the process that collects the data for this plugin. (win10).
    – bvdb
    Commented Apr 1, 2021 at 11:24
10

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 );
        }
    });
});
3
  • 1
    FYI: the arguments function looks for processes started with those arguments. For eg, "node myScript.js --debug" Commented Mar 23, 2015 at 22:22
  • The selected answer on this page explains the basic concept behind ps. Adding support for other operating systems looks relatively easy. github.com/neekey/ps/blob/master/lib/index.js
    – Eddie
    Commented Mar 15, 2016 at 6:20
  • 1
    I just want to point out that ps-node is unreliable and should never be used in production or where you are relying it seriously. Why? It has known issues parsing the process listing table. This is unpredictable (i.e. it depends on what processes are running) and has bit me in the behind more than once. You have been warned.
    – logidelic
    Commented Feb 26, 2019 at 17:30
6

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

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

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

Solution for unix-like systems:

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

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 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
1

Use the command line tools rather than node scripts.

ps -aef | grep node

This would list the visual studio code helpers and .nvm env processes. We can exclude them using

ps -aef | grep node | grep -v "Visual Studio" | grep -v ".nvm"

Additional tip to kill all the listed processes:

killall node
0

Seems there isn't any direct methods

but below videos might help.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.