17

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);
Alexis Wilke
  • 15,168
  • 8
  • 60
  • 116
Asim Zaidi
  • 23,590
  • 46
  • 125
  • 213

6 Answers6

20

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.
});
BadCanyon
  • 2,697
  • 15
  • 17
  • 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 Dec 27 '17 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 – decoder7283 Apr 11 '18 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 Sep 10 '20 at 12:02
8

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 );
        }
    });
});
Eddie
  • 1,119
  • 10
  • 21
  • 1
    FYI: the arguments function looks for processes started with those arguments. For eg, "node myScript.js --debug" – Gideon Rosenthal Mar 23 '15 at 22:22
  • The selected answer on this page explains the basic concept behind ps. Adding support for other operating systems looks relatively easy. https://github.com/neekey/ps/blob/master/lib/index.js – Eddie Mar 15 '16 at 06:20
  • 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 Feb 26 '19 at 17:30
7

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'}, ...] 
});
KARASZI István
  • 28,974
  • 8
  • 95
  • 116
  • Doesn't support a bunch of fields though - only the basics. – vaughan Aug 05 '19 at 22:32
  • You may also want to filter out the `name === 'fastlist...'` process which is the process that collects the data for this plugin. (win10). – bvdb Apr 01 '21 at 11:24
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 } }]
shadow0359
  • 892
  • 14
  • 24
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
PADYMKO
  • 3,291
  • 2
  • 31
  • 36
0

Seems there isn't any direct methods

but below videos might help.

oyalhi
  • 3,417
  • 4
  • 32
  • 41
Hitesh Chavda
  • 752
  • 1
  • 9
  • 25