GithubHelp home page GithubHelp logo

ps's Introduction

ps Build Status Build status

A Node.js module for looking up running processes. This module uses Table-Parser to parse the output.

Before using this module, you should take look at section Existing Bugs You Should Know at the bottom of this doc.

Install

$ npm install ps-node

How Does It Work

This module uses different tools to get process list:

  • Linux / Mac: use ps command. Since the default result from shell command $ ps will not contain "command arguments" in linux like "ubuntu", ps-node add arguments lx as default. Which means, the default value for option psargs is lx.
  • Win: use command wmic process get ProcessId,CommandLine through "cmd", more info about wmic is here. Anyway, there is also another tool name tasklist in windows, which can also list all the running processes, but lack of command arguments infomation. But compared to wmic, I think this tool should have a higher performance. You should take a look at the wrapper for this tool tasklist by @sindresorhs if you are interested.

Compatibility

  • Should work great in most *nix system.
  • Should work on Win10/7 more system versions need to be test.

Any compatibility issue is welcomed.

Usage

Lookup process with specified pid:

var ps = require('ps-node');

// A simple pid lookup
ps.lookup({ pid: 12345 }, function(err, resultList ) {
    if (err) {
        throw new Error( err );
    }

    var process = resultList[ 0 ];

    if( process ){

        console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
    }
    else {
        console.log( 'No such process found!' );
    }
});

Or use RegExp to filter command and arguments:

var ps = require('ps-node');

// A simple pid lookup
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 );
        }
    });
});

Also, you can use kill to kill process by pid:

var ps = require('ps-node');

// A simple pid lookup
ps.kill( '12345', function( err ) {
    if (err) {
        throw new Error( err );
    }
    else {
        console.log( 'Process %s has been killed!', pid );
    }
});

Method kill also supports a signal option to be passed. It's only a wrapper of process.kill() with checking of that killing is finished after the method is called.

var ps = require('ps-node');

// Pass signal SIGKILL for killing the process without allowing it to clean up
ps.kill( '12345', 'SIGKILL', function( err ) {
    if (err) {
        throw new Error( err );
    }
    else {
        console.log( 'Process %s has been killed without a clean-up!', pid );
    }
});

you can use object as the second parameter to pass more options:

ps.kill( '12345', { 
    signal: 'SIGKILL',
    timeout: 10,  // will set up a ten seconds timeout if the killing is not successful
}, function(){});

Notice that the nodejs build-in process.kill() does not accept number as the signal, you will have to use string format.

You can also pass arguments to lookup with psargs as arguments for ps command(Note that psargs is not available in windows):

var ps = require('ps-node');

// A simple pid lookup
ps.lookup({
    command: 'node',
    psargs: 'ux'
    }, 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 );
        }
    });
});

Lastly, you can filter a list of items by their PPID by passing a PPID to filter on. You will need to pass in a psarg that provides the PPID in the results (-l or -j for instance).

var ps = require('ps-node');

// A simple pid lookup
ps.lookup({
    command: 'mongod',
    psargs: '-l',
    ppid: 82292
    }, 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 );
        }
    });
});

Existing Bugs You Should Know

I'm still working on these bugs at the moment, before using this module in any serious way, please take a look at them, and take your own risk.

ps's People

Contributors

cosminlupu avatar dazaar avatar emlama avatar empact avatar jayfunk avatar jrust avatar kbjr avatar marcusoftnet avatar mayank-patel avatar neekey avatar yetzt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ps's Issues

Use spawn instead of exec

Hi,
I was debugging my app and found out that I run lot of processes on my pc and the output from ps -A exceeded 200kB, which is default buffer size for child_process.exec function, and then child process was killed. I was wondering why not to use child_process.spawn when it is already used for win branch.

I saw it as change to contribute to open sourced package I use and tried to implement it using spawn and it worked or at least it helped to solve my problem. Here's pull request in my forked version Use spawn instead of exec.

While it works 'on my machine', it randomly fails with different node versions on travis. Have no idea why. I tested it with more node versions on my machine and it still worked.

So, is there any particular reason to not use child_process.spawn ? Do you have any idea what could be the issue with my solution?

PS. Increasing buffer size for exec could also help, but I'm not fun of such solutions.

Martin

Regression introduced with v0.1.5 with BusyBox v1.22.1

I'm using this library on an Intel Galileo,
Which is running BusyBox v1.22.1 (2015-10-25 11:24:34 GMT) multi-call binary.

          var psNode = require('ps-node');

            psNode.lookup({ pid: pId }, function(err, resultList) {
                if(err) return reject(err);
                return done(resultList[0]);
            });

When I pass a running pId to the lookup function in v0.1.4, the resultList will return an array with
the running process details.

When I pass a running pId to the lookup function in v0.1.5 the resultList will return an empty array.

ps usage options on busybox a.k.a none

root@hostname:~/peter/ps-mon# ps --help
BusyBox v1.22.1 (2015-10-25 11:24:34 GMT) multi-call binary.

Usage: ps 

ps table fields on BusyBox

PID USER VSZ STAT COMMAND

Can't list process on a 64bit Windows 7 Pro (french locale)

I have the error process - cannot find alias on the wmic process (in french process - Alias introuvable.).

I managed to get it working again by changing line 67 from

CMD.stdin.write('wmic process get ProcessId,ParentProcessId,CommandLine \n');

to

CMD.stdin.write('wmic path win32_process get ProcessId,ParentProcessId,CommandLine \n');

According the the command line, it may only return 32 bit processes, but it works for me. As I'm not a Windows user, I can't help more, sorry for that...

ps lookup returning false data

I use ps-node to check if a process is still running and if not restart it. Every now and then it returns an empty array without an error. At the end of a day, about 3 processes are running. I didn't find a workaround yet

On Linux, all PIDs >= 1000 are '?'

I'm not sure why, but on my Debian (jessie) system, all PIDs >= 1000 get parsed by this module as '?'. Everything under 1000 works fine.

It looks like TableParser is combining the PID and TTY fields in some cases. Here's the objects as they come out of TableParser:

{ PID: [ '863' ],
  TTY: [ 'tty1' ],
  STAT: [ 'S+' ],
  TIME: [ '0:00' ],
  COMMAND: [ '-bash' ] }
{ PID: [ '996', '?' ],
  STAT: [ 'Ss' ],
  TIME: [ '0:00' ],
  COMMAND: [ '/bin/sh', '-c', '~/7dtd-agent/systemd/7dtd.sh' ] }
{ PID: [ '997', '?' ],
  TTY: [ 'S' ],
  TIME: [ '0:00' ],
  COMMAND: [ '/bin/sh', '/home/chris/7dtd-agent/systemd/7dtd.sh' ] }
{ PID: [ '998', '?' ],
  TTY: [ 'S' ],
  TIME: [ '0:00' ],
  COMMAND: [ '/bin/sh', './startserver.sh', '-configfile=serverconfig.xml' ] }
{ PID: [ '?' ],
  STAT: [ 'Sl' ],
  TIME: [ '15:46' ],
  COMMAND:
   [ './7DaysToDieServer.x86_64',
     '-logfile',
     '7DaysToDieServer_Data/output_log__2016-10-27__07-40-43.txt',
     '-quit',
     '-batchmode',
     '-nographics',
     '-dedicated',
     '-configfile=serverconfig.xml' ] }
{ PID: [ '?' ],
  TTY: [ 'S' ],
  TIME: [ '0:00' ],
  COMMAND: [ 'sshd:', 'chris@pts/0' ] }
{ TTY: [ 'pts/0' ],
  STAT: [ 'Ss' ],
  TIME: [ '0:00' ],
  COMMAND: [ '-bash' ] }
{ TTY: [ 'pts/0' ],
  STAT: [ 'Sl+' ],
  TIME: [ '0:00' ],
  COMMAND: [ 'node', 'index.js' ] }
{ TTY: [ 'pts/0' ],
  STAT: [ 'S+' ],
  TIME: [ '0:00' ],
  COMMAND: [ '/bin/sh', '-c', 'ps', 'x' ] }
{ TTY: [ 'pts/0' ],
  STAT: [ 'R+' ],
  TIME: [ '0:00' ],
  COMMAND: [ 'ps', 'x' ] }

Note that the second object is already incorrect -- the PID and TTY columns are combined. The fifth object, the one I care about, doesn't even retain the PID value that I care about (maybe because it's longer than three characters).

Can't kill child processes anymore

ps-node seems to get stuck in an infinite loop. This change was introduced in v0.1.5. Pinning our version to v0.1.4 seem to fix the issue.

Here is where we make a call to ps-node:
https://github.com/Galooshi/happo/blob/c0edfc6353a58d369cc4ea2de6c4e017583f8b6a/src/server/closeDriver.js#L31

Whereas previously this ran successfully, now we get an infinite loop of the following message:

check kill success 2079 confident 0
check kill success result: 1
check kill success 2079 confident 0
check kill success result: 1
check kill success 2079 confident 0
check kill success result: 1
check kill success 2079 confident 0
check kill success result: 1
check kill success 2079 confident 0
check kill success result: 1
check kill success 2079 confident 0
check kill success result: 1
...

FYI @lencioni @trotzig

No such process found!

Following the code:

let childWorker = cp.fork(`${__dirname}/../somefile`);

console.log(childWorker.pid);
// 12345

ps.lookup({ pid: childWorker.pid }, (err, resultList) => {
    console.log(resultList);
    // []
});

Why it didn't work? I also randomly picked pid from ps -A and it still didn't work.

stdout of ChildProcess.exec("ps") is cut short not giving the complete args to process

Found on version 0.1.4
/lib/index.js Line 74

If a process is started with a particularly long argument/ multiple arguments,

e.g.node /really/long/absolute/path/to/my/script/myscriptname.js

The object returned ps.lookup for that process will return a truncated version of the
argument passed to the script e.g. /really/long/absolute/pa

` else {
ChildProcess.exec('ps ' + args, function (err, stdout, stderr) {

  if (err || stderr) {
    return callback(err || stderr.toString());
  }
  else {
    stdout = stdout.toString();
    callback(null, stdout || false);
  }
});`

Function that is causing the problem is the ChildProcess.exec.
Suggested solutions are to change from exec to spawn?

http://www.hacksparrow.com/difference-between-spawn-and-exec-of-node-js-child_process.html

Minor workaround is is to pass an option of { psargs: " " } to the lookup function to limit the size of
stdout returned

'wmic' is not recognized as an internal or external command

$ npm install -g ps-node
$ node
> const ps = require("ps-node")
undefined
> ps.lookup({command: "node"}, (err, data) => console.dir({err, data}))
undefined
> { err: '\'wmic\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n',
  data: undefined }

Windows 10 build 15063, node v7.4.0, running in a git-bash shell

TableParser parsing data incorrectly

ps-node version: "0.1.6"
table-parser version: "0.1.3"

I've been running a node process for a while now without problems that uses ps-node every few minutes. But I also run a few other CPU intensive processes on the machine.

One day it stopped working, maybe due to PIDs, used memory, or total CPU time getting too high. I didn't step through TableParser to find where it goes wrong. I've supplied some code here with my output (with some process names and arguments redacted) that will recreate the problem.

Maybe it's time to switch from Unix ps to using something like:
https://www.npmjs.com/package/procps

// Output copied from calling console.log() within ps-node/index.js's parseGrid function 
var psoutput = `F   UID   PID  PPID PRI  NI    VSZ   RSS WCHAN  STAT TTY        TIME COMMAND
4  1000  3002     1  20   0  45248     0 ep_pol Ss   ?          0:00 /lib/systemd/systemd --user
5  1000  3006  3002  20   0 163688     4 -      S    ?          0:00 (sd-pam)
0  1000  3101     1  20   0 9657524 6355656 poll_s Sl ?       1440:57 mysqld --datadir /aa/aa/aa
0  1000  3178     1  20   0 808752 247892 hrtime Sl  ?        11234:03 ./aa -aa /aa/aa/aa/aa.aa -aa /aa/aa/aa/aa/aa/aa/aa -aa /aa/aa/aa/aa/aa/bb/bb.bb.bb.bb -t 4 -bb /cc/cc/cc/cc/cc/cc/cc.cc -cc /a/a/a/a/a/a/a.a
0  1000  3198     1  20   0 639312     0 hrtime Sl   ?          3:54 ./h -d /a/a/a -p 3
0  1000  9215     1  20   0 1879292 95352 ep_pol Sl  ?         47:06 node ./d/d
0  1000 12547     1  20   0 1931424 21016 ep_pol Sl  ?          0:14 node app.js --prod
0  1000 17244     1  20   0 1450676 190724 ep_pol Sl ?        226:10 node app.js --prod
1  1000 17789     1  20   0  79940 44376 -      S    ?         17:43 tor --runasdaemon 1
5  1000 21352 21325  20   0 113860  1236 -      S    ?          0:01 sshd: user@pts/8
0  1000 21353 21352  20   0  22676  3804 wait_w Ss+  pts/8      0:00 -bash
5  1000 21675 21647  20   0 113868  1232 -      S    ?          0:00 sshd: user@pts/9
0  1000 21676 21675  20   0  22788  4748 wait   Ss   pts/9      0:00 -bash
0  1000 21973 21676  20   0 920496 28816 ep_pol Sl+  pts/9      0:00 node
0  1000 21987 21973  20   0  28916  1500 -      R+   pts/9      0:00 ps lx`;

// Try parsing and view the output
var TableParser = require('table-parser');
var garbledTable = TableParser.parse(psoutput);
console.log(garbledTable);

Win10: System processes are reported with pid and ppid, but with no command or arguments

Here is what I am getting on Windows 10:

{ pid: '0', command: '', arguments: '', ppid: '0' }
{ pid: '4', command: '', arguments: '', ppid: '0' }
{ pid: '412', command: '', arguments: '', ppid: '4' }
{ pid: '580', command: '', arguments: '', ppid: '492' }
{ pid: '684', command: '', arguments: '', ppid: '492' }
{ pid: '692', command: '', arguments: '', ppid: '676' }
{ pid: '760', command: '', arguments: '', ppid: '684' }
{ pid: '792', command: '', arguments: '', ppid: '684' }
{ pid: '864', command: '', arguments: '', ppid: '676' }
{ pid: '968', command: '', arguments: '', ppid: '760' }
{ pid: '996', command: '', arguments: '', ppid: '760' }
{ pid: '1020', command: '', arguments: '', ppid: '864' }
{ pid: '96', command: '', arguments: '', ppid: '684' }
{ pid: '756', command: '', arguments: '', ppid: '760' }
{ pid: '496', command: '', arguments: '', ppid: '760' }
{ pid: '1088', command: '', arguments: '', ppid: '864' }
{ pid: '1184', command: '', arguments: '', ppid: '760' }
{ pid: '1200', command: '', arguments: '', ppid: '760' }
{ pid: '1268', command: '', arguments: '', ppid: '760' }
{ pid: '1340', command: '', arguments: '', ppid: '760' }
{ pid: '1376', command: '', arguments: '', ppid: '760' }
{ pid: '1384', command: '', arguments: '', ppid: '1200' }
{ pid: '1400', command: '', arguments: '', ppid: '760' }
{ pid: '1416', command: '', arguments: '', ppid: '760' }
{ pid: '1424', command: '', arguments: '', ppid: '760' }
{ pid: '1620', command: '', arguments: '', ppid: '760' }
{ pid: '1732', command: '', arguments: '', ppid: '760' }
{ pid: '1772', command: '', arguments: '', ppid: '760' }
{ pid: '1808', command: '', arguments: '', ppid: '760' }
{ pid: '1920', command: '', arguments: '', ppid: '760' }
{ pid: '1972', command: '', arguments: '', ppid: '1200' }
{ pid: '2008', command: '', arguments: '', ppid: '760' }
{ pid: '2020', command: '', arguments: '', ppid: '760' }
{ pid: '1036', command: '', arguments: '', ppid: '760' }
{ pid: '1064', command: '', arguments: '', ppid: '760' }
{ pid: '2076', command: '', arguments: '', ppid: '760' }
{ pid: '2104', command: '', arguments: '', ppid: '760' }
{ pid: '2164', command: '', arguments: '', ppid: '760' }
{ pid: '2216', command: '', arguments: '', ppid: '1036' }
{ pid: '2256', command: '', arguments: '', ppid: '760' }
{ pid: '2420', command: '', arguments: '', ppid: '760' }
{ pid: '2508', command: '', arguments: '', ppid: '760' }
{ pid: '2564', command: '', arguments: '', ppid: '760' }
{ pid: '2596', command: '', arguments: '', ppid: '2508' }
{ pid: '2620', command: '', arguments: '', ppid: '760' }
{ pid: '2628', command: '', arguments: '', ppid: '760' }
{ pid: '2804', command: '', arguments: '', ppid: '760' }
{ pid: '2828', command: '', arguments: '', ppid: '760' }
{ pid: '4672', command: '', arguments: '', ppid: '760' }
{ pid: '4684', command: '', arguments: '', ppid: '760' }
{ pid: '5196', command: '', arguments: '', ppid: '4' }
{ pid: '6012', command: '', arguments: '', ppid: '760' }
{ pid: '6196', command: '', arguments: '', ppid: '996' }
{ pid: '7088', command: '', arguments: '', ppid: '760' }
{ pid: '7660', command: '', arguments: '', ppid: '760' }
{ pid: '7704', command: '', arguments: '', ppid: '760' }
{ pid: '8136',
  command: 'C:\\Program Files (x86)\\HitmanPro.Alert\\hmpalert.exe',
  arguments: [ '/tray' ],
  ppid: '1920' }
{ pid: '8180',
  command: 'C:\\Windows\\TEMP\\DPTF\\esif_assist_64.exe',
  arguments: '',
  ppid: '4084' }
{ pid: '6680',
  command: 'sihost.exe',
  arguments: '',
  ppid: '1772' }
{ pid: '8300',
  command: 'C:\\Windows\\Explorer.EXE',
  arguments: '',
  ppid: '9084' }
{ pid: '9120',
  command: 'igfxEM.exe',
  arguments: '',
  ppid: '9104' }
{ pid: '7908',
  command: 'igfxHK.exe',
  arguments: '',
  ppid: '9104' }
{ pid: '8712',
  command: 'igfxTray.exe',
  arguments: '',
  ppid: '9104' }
{ pid: '9208',
  command: 'c:\\windows\\system32\\svchost.exe',
  arguments: [ '-k', 'unistacksvcgroup', '-s', 'CDPUserSvc' ],
  ppid: '760' }
{ pid: '7084',
  command: 'C:\\Program Files\\Synaptics\\SynTP\\SynTPEnh.exe',
  arguments: '',
  ppid: '4536' }
{ pid: '704',
  command: 'c:\\windows\\system32\\svchost.exe',
  arguments: [ '-k', 'unistacksvcgroup', '-s', 'WpnUserService' ],
  ppid: '760' }
{ pid: '1788', command: '', arguments: '', ppid: '760' }
{ pid: '8332', command: '', arguments: '', ppid: '1620' }
{ pid: '2820',
  command: 'taskhostw.exe',
  arguments: [ '{222A245B-E637-4AE9-A93F-A59CA119A75E}' ],
  ppid: '1620' }
{ pid: '9376', command: '', arguments: '', ppid: '760' }
{ pid: '9660', command: '', arguments: '', ppid: '1232' }
{ pid: '9668', command: '', arguments: '', ppid: '1668' }
{ pid: '9680', command: '', arguments: '', ppid: '1668' }
{ pid: '9812',
  command: 'C:\\Windows\\SystemApps\\ShellExperienceHost_cw5n1h2txyewy\\ShellExperienceHost.exe',
  arguments: [ '-ServerName:App.AppXtk181tbxbce2qsex02s8tw7hfxa9xb3t.mca' ],
  ppid: '996' }
{ pid: '10116', command: '', arguments: '', ppid: '760' }
{ pid: '9556',
  command: 'C:\\Windows\\System32\\RuntimeBroker.exe',
  arguments: [ '-Embedding' ],
  ppid: '996' }
{ pid: '1668', command: '', arguments: '', ppid: '760' }
{ pid: '8672',
  command: 'C:\\Program Files\\Windows Defender\\MSASCuiL.exe',
  arguments: '',
  ppid: '8300' }
{ pid: '10784',
  command: 'C:\\Program Files\\Realtek\\Audio\\HDA\\RtkNGUI64.exe',
  arguments: [ '-s' ],
  ppid: '8300' }
{ pid: '11036',
  command: 'C:\\Program Files\\Realtek\\Audio\\HDA\\RAVBg64.exe',
  arguments: [ '/IM' ],
  ppid: '8300' }
{ pid: '11016',
  command: 'C:\\Windows\\system32\\wbem\\unsecapp.exe',
  arguments: [ '-Embedding' ],
  ppid: '996' }
{ pid: '1108', command: '', arguments: '', ppid: '996' }
{ pid: '11280', command: '', arguments: '', ppid: '11268' }
{ pid: '11368',
  command: 'C:\\Program Files\\Alienware\\Command Center\\AWCCServiceController.exe',
  arguments: '',
  ppid: '11292' }
{ pid: '11420', command: '', arguments: '', ppid: '11280' }
{ pid: '11440', command: '', arguments: '', ppid: '11280' }
{ pid: '11500',
  command: 'C:\\Program Files\\Sophos\\Sophos UI\\Sophos UI.exe',
  arguments: [ '/hidden' ],
  ppid: '8300' }
{ pid: '11760', command: '', arguments: '', ppid: '11292' }
{ pid: '11916',
  command: 'C:\\Program Files\\Box\\Box Sync\\BoxSync.exe',
  arguments: [ '-m' ],
  ppid: '8300' }
{ pid: '12072',
  command: 'C:\\Program Files\\iTunes\\iTunesHelper.exe',
  arguments: '',
  ppid: '8300' }
{ pid: '11316', command: '', arguments: '', ppid: '760' }
{ pid: '11628',
  command: 'C:\\Program Files (x86)\\MySQL\\MySQL Notifier 1.1\\MySQLNotifier.exe',
  arguments: '',
  ppid: '8300' }
{ pid: '11712',
  command: 'C:\\Program Files\\Killer Networking\\Killer Control Center\\KillerControlCenter.exe',
  arguments: [ '-minimized' ],
  ppid: '8300' }
{ pid: '12048',
  command: 'C:\\Program Files (x86)\\BeAnywhere Support Express\\GetSupportService_N-Central\\BASupSrvcCnfg.exe',
  arguments: [ '/silent' ],
  ppid: '10052' }
{ pid: '12268',
  command: 'C:\\Program Files\\Box\\Box Sync\\BoxSyncMonitor.exe',
  arguments: [ '-l', '75', '-p', '11916' ],
  ppid: '11916' }
{ pid: '9112',
  command: '\\??\\C:\\Windows\\system32\\conhost.exe',
  arguments: [ '0x4' ],
  ppid: '12268' }
{ pid: '11732',
  command: 'C:\\Program Files (x86)\\Common Files\\Java\\Java Update\\jusched.exe',
  arguments: '',
  ppid: '10052' }
{ pid: '712',
  command: 'C:\\Program Files\\Alienware\\Command Center\\AlienwareAlienFXController.exe',
  arguments: '',
  ppid: '11292' }
{ pid: '1660',
  command: 'C:\\Program Files\\Alienware\\Command Center\\AlienFusionController.exe',
  arguments: '',
  ppid: '712' }
{ pid: '12736',
  command: 'C:\\Program Files\\Alienware\\Command Center\\AWCCApplicationWatcher32.exe',
  arguments: [ '262326' ],
  ppid: '11368' }
{ pid: '12752',
  command: '\\??\\C:\\Windows\\system32\\conhost.exe',
  arguments: [ '0x4' ],
  ppid: '12736' }
{ pid: '12764',
  command: 'C:\\Program Files\\Alienware\\Command Center\\AWCCApplicationWatcher64.exe',
  arguments: [ '262326' ],
  ppid: '11368' }
{ pid: '12776',
  command: '\\??\\C:\\Windows\\system32\\conhost.exe',
  arguments: [ '0x4' ],
  ppid: '12764' }
{ pid: '12568',
  command: 'C:\\Program Files (x86)\\Cisco\\Cisco AnyConnect Secure Mobility Client\\vpnui.exe',
  arguments: [ '-minimized' ],
  ppid: '10052' }
{ pid: '816', command: '', arguments: '', ppid: '760' }
{ pid: '13788', command: '', arguments: '', ppid: '760' }
{ pid: '6140', command: '', arguments: '', ppid: '760' }
{ pid: '3768', command: '', arguments: '', ppid: '760' }
{ pid: '10024', command: '', arguments: '', ppid: '760' }
{ pid: '14380',
  command: 'c:\\windows\\system32\\svchost.exe',
  arguments: [ '-k', 'unistacksvcgroup' ],
  ppid: '760' }
{ pid: '15196',
  command: 'C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\OUTLOOK.EXE',
  arguments: '',
  ppid: '8300' }
{ pid: '11256',
  command: 'C:\\Program Files\\Internet Explorer\\iexplore.exe',
  arguments: [ '-startmanager', '-Embedding' ],
  ppid: '996' }
{ pid: '13808',
  command: 'C:\\Program Files (x86)\\Internet Explorer\\IEXPLORE.EXE',
  arguments: [ 'SCODEF:11256', 'CREDAT:75009', '/prefetch:2' ],
  ppid: '11256' }
{ pid: '12032',
  command: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
  arguments: [ '--allow-running-insecure-content', '--disable-web-security' ],
  ppid: '8300' }
{ pid: '14984',
  command: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
  arguments: 
   [ '--type=crashpad-handler',
     '--user-data-dir=C:\\Users\\Mark Jeghers\\AppData\\Local\\Google\\Chrome\\User Data',
     '/prefetch:7',
     '--monitor-self-annotation=ptype=crashpad-handler',
     '--database=C:\\Users\\Mark Jeghers\\AppData\\Local\\Google\\Chrome\\User Data\\Crashpad',
     '--metrics-dir=C:\\Users\\Mark Jeghers\\AppData\\Local\\Google\\Chrome\\User Data',
     '--url=https://clients2.google.com/cr/report',
     '--annotation=channel=',
     '--annotation=plat=Win64',
     '--annotation=prod=Chrome',
     '--annotation=ver=62.0.3202.94',
     '--initial-client-data=0x278,0x27c,0x280,0x274,0x284,0x7ff8d14027e8,0x7ff8d14027a8,0x7ff8d14027b8' ],
  ppid: '12032' }
{ pid: '6044',
  command: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
  arguments: 
   [ '--type=watcher',
     '--main-thread-id=14628',
     '--on-initialized-event-handle=744',
     '--parent-handle=748',
     '/prefetch:6' ],
  ppid: '12032' }
etc etc etc

Travis test cases fail for Mac OS X

Did some debugging, seems like the reason for failing is more because the ps output from its test environment does not contain any node related process, only like two processes used for the test itself.

So will have to remove test configs for Mac at the moment, until I find a way to work around it.

next is not a function

When I use the kill command I get the exception below. It appears that if the kill function does not have a callback then it will attempt to call the callback. If I have some time Ill try and put in a PR.

Code:

ps.lookup({command: 'test.exe'}, (err, results) => {
  results.forEach(result => {
      if (result.pid) {
        console.log(`Killing process ID: ${result.pid}`);
        ps.kill(result.pid);
      }
  });
});

Error Message:

Killing container process ID: 31244
C:\Users\tcook\projects\performance-test-runner\node_modules\ps-node\lib\index.js:190
                    next( null, stdout );
                    ^

TypeError: next is not a function
    at Timeout._onTimeout (C:\Users\tcook\projects\performance-test-runner\node_modules\ps-node\lib\index.js:190:21)
    at tryOnTimeout (timers.js:224:11)
    at Timer.listOnTimeout (timers.js:198:5)

Promises support

Please add a possibility for public functions to return promises if callback function is not provided. For example:

ps.lookup({ pid: 12345 }, function(err, resultList ) {}) =======> ps.lookup({ pid: 12345 }) -> Promise<resultList>

Oddly bad performance

OS: Win7 x64

wmi-client x 6.75 ops/sec ±3.90% (36 runs sampled)
process-list x 9.97 ops/sec ±2.53% (51 runs sampled)
ps-node x 0.07 ops/sec ±1.18% (5 runs sampled)

Benchmark setup, see: https://gist.github.com/s-h-a-d-o-w/c0b7ee374db044a21a6b5b1e99d269f0

Something can't be right here, considering that ps-node uses wmic internally. So the result should at least be somewhat similar to using wmic a bit more directly via wmi-client.

Not responding windows 10

I've tested my code on linux, and it works great, but when I try to run the same code on windows 10, my application stops responding.

function findProcess(name, callback)
{
  ps.lookup(
  {
    command: name
  }, function(err, resultList)
  {
    if(err)
    {
      throw new Error(err);
    }

    callback(resultList.length > 0);
    
  });
}

wrong split command and arguments if path contains spaces

A issue reported:

I’m porting my program to osX, and found a bug in ps-node.

On osX, commands path may contain spaces. For instance I’m trying to find ocurrences of Google Chrome, and here is an example of ‘ps -A’ command output:

Mac-Mini:herve$ ps -A
  PID TTY           TIME CMD
54292 ??         0:01.00 /Applications/Google Chrome.app/Contents/Versions/47.0
54359 ??         0:02.96 /Applications/Google Chrome.app/Contents/Versions/47.0
54391 ??         0:01.41 /Applications/Google Chrome.app/Contents/Versions/47.0
54694 ??         0:00.87 /Applications/Google Chrome.app/Contents/Versions/47.0

See the space between ‘Google' and ‘Chrome’?

You obviously use the space to separate the command from its arguments, which is wrong on osX. The resulting object of your parsing contains (for instance):

{ pid: '54359',
  command: '/Applications/Google',
  arguments: 
   [ 'Chrome.app/Contents/Versions/47.0.2526.106/Google',
     'Chrome',
     'Helper.app/Contents/MacOS/Google',
     'Chrome',
     'Helper',
     '--type=renderer',
     '--enable-experimental-web-platform-features',
     '--lang=fr' ],
  ppid: undefined }

I suppose that you could replace the line in table-parser/lib/index.js

var fields = line.split( /\s+/ );

by something like:

var fields = line.split( /[^\/]\s+[^\/]/ ); // not tested so it doesn’t work :-)

to eliminate the spaces between slashes inside fields. This may benefit to arguments too, since they can contain a path with a space.

I didn’t check the bug on previous versions of ps-node.

Command failed: ps l

I'm having this error:
{[Error: Command failed: ps l ] killed: false, code: 1, signal: null, cmd: 'ps l' }


Error: Error: Command failed: ps l

    at /usr/src/app/run.js:78:27
    at /usr/src/app/node_modules/ps-node/lib/index.js:139:20
    at /usr/src/app/node_modules/ps-node/lib/index.js:76:24
    at ChildProcess.exithandler (child_process.js:209:5)
    at emitTwo (events.js:100:13)
    at ChildProcess.emit (events.js:185:7)
    at maybeClose (internal/child_process.js:850:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5)

node v5.10.0

Any idea?
Thanks

Get the path of the running process

I have two nginx executables running.

  1. C:/nginx/nginx.exe (or say usr/bin)
  2. C:/second/nginx/nginx.exe (or say $home/local/nginx)

I wish to get the executable's path of the running nginx file whether it is the first or second. I am aware of this command wmic process where "ExecutablePath like 'c:\\nginx\\nginx%'" get ProcessID in windows. Is there a way I can do it using ps-node so it applies to both linux and win?

var ps = require('ps-node');
ps.lookup({
    command: 'nginx',
    psargs: 'ux'
    }, function(err, resultList ) {
    if (err) {
        throw new Error( err );
    }
    resultList.forEach(function( process ){
        if( process ){
            console.log(process.pid, process.command, **process.ExecutablePath**, process.arguments );
            // **process.ExecutablePath** => c:/nginx or usr/bin
        }
    });
});

Can't get results from some searches

Hi, this is more a problem making the search than a bug:
O.S: Lubuntu
Node version: 6.11.0
ps version: 0.1.6

ps.lookup({
        command: "chromium-browser"
    }, function(err, resultList) {
   console.log(resultList.length);
});

I get 0. If i run ps -C chromium-browser i do get results:

osboxes@osboxes:~$ ps -C chromium-browser
  PID TTY          TIME CMD
 1979 ?        00:23:34 chromium-browse
 1987 ?        00:00:00 chromium-browse
 1989 ?        00:00:00 chromium-browse
 2450 ?        00:00:03 chromium-browse
 2470 ?        00:03:02 chromium-browse
 2483 ?        00:00:40 chromium-browse
 3490 ?        00:00:02 chromium-browse
 3492 ?        00:00:00 chromium-browse
 5775 pts/0    00:00:01 chromium-browse
 5784 pts/0    00:00:00 chromium-browse
 5786 pts/0    00:00:00 chromium-browse

If i search for node i do get results.

¿What commad does exactly ps run?

It should show get pid but get username instead

line 221:
var pid = ( d.PID && d.PID[ 0 ] ) || ( d.ProcessId && d.ProcessId[ 0 ] ) || undefined;
should be:
var pid = ( d.PID && d.PID[ 1 ] ) || ( d.ProcessId && d.ProcessId[ 1 ] ) || undefined;
In order to get pid instead of username.

Not sure if this issue happen because of system dependency(I use Mac OS X and Ubuntu MATE).
Or should I send a PR directly?

Show all processes in ubuntu.

exports.lookup = function (query, callback) {
-line var exeArgs = query.psargs || ['l'];
+line var exeArgs = query.psargs || ['lx'];

Without the x only processes with a tty will show.

Using the default "l" command caused the table to become skewed by TableParser

I got bad output using OS X Yosemite version 10.10.5 (14F27) with this doing a simple PID lookup. When I removed the "l" parameter that is hardcoded in, I was able to get the proper output, even though it's much less output.

I don't know if anyone else has experienced this. But I have version 0.1.2 installed.

old output of a process:
UID: [ '407128372' ],
PID: [ '2', '70505' ],
PPID: [ '70505', '70504' ],
CPU: [ '70504' ],
PRI: [ '0' ],
NI: [ '31', '0' ],
VSZ: [ '2464092' ],
RSS: [ '692' ],
WCHAN: [ '692', '-' ],
STAT: [ 'S+' ],
TT: [ '+', 's003' ],
TIME: [ '0:00.05' ],
COMMAND: [ '0:00.05', '-bash' ] }

"fixed" out put of a process:
{ PID: [ '80018' ],
TTY: [ 'ttys003' ],
TIME: [ '0:00.53' ],
CMD: [ 'node', 'node-site.jslib' ] }

Silently returns incorrect results sporadically (Ubuntu)

I have been using ps-node in production for about a year. I have experienced some mystery issues very rarely but never investigated closely enough until today. Finally I realize that, rarely, ps-node is failing to parse the process list correctly and is therefor returning incorrect results.

I know that there is a note about "limitations" for multiple-bytes characters. Is it possible that there was some other process in the system that was using a multi-byte character? Sure, how would I know? The point is that users can't know this, so the "limitation" should be rephrased as, "ps-node is unreliable and sometimes it returns incorrect information". I think this needs to be put up front, at the very top of the readme.

To summarize: ps-node is not to be used anywhere that reliability is important.

I still appreciate the work done by the module writers... I understand that parsing the command-line output is problematic and maybe even impossible in this case. I just think that users should understand this...

For what it's worth, below is an exerpt of what ps-node returned to me in the problem case. I haven't been able to repro since then so I don't know what tickled it. Note that instead of the actual command, some incorreclty-parsed digits are returned:

[
    {
        "pid": "1314",
        "command": "0:00",
        "arguments": [
            "/lib/systemd/systemd-logind"
        ],
        "ppid": "1"
    },
    {
        "pid": "1324",
        "command": "0:20",
        "arguments": [
            "/usr/lib/snapd/snapd"
        ],
        "ppid": "1"
    },
    {
        "pid": "1346",
        "command": "0:00",
        "arguments": [
            "/usr/sbin/sshd",
            "-D"
        ],
        "ppid": "1"
    },
    {
        "pid": "1441",
        "command": "0:00",
        "arguments": [
            "/sbin/agetty",
            "--keep-baud",
            "115200",
            "38400",
            "9600",
            "ttyS0",
            "vt220"
        ],
        "ppid": "1"
    },
    {
        "pid": "1444",
        "command": "0:00",
        "arguments": [
            "/sbin/agetty",
            "--noclear",
            "tty1",
            "linux"
        ],
        "ppid": "1"
    }
]

Need more data through windows command

as I've read in #10 , there can be use of the command:

wmic process list

instead of:

wmic process get ProcessId,CommandLine

the list command returns much more data which personally, I'd like to get.

is there any possibility to change the implementation in such a way that I could choose the projection I'd want out of all the available data that wmic offers?

PS: I'm not using wmic directly because I need it to run both on win & *nix systems

Feature request: PPID for Windows

Seems like Parent PID is not supported in Windows.
It could be added using this command:

wmic process get processid,parentprocessid

Please ask if you need PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.