Coder Social home page Coder Social logo

apify / ps-tree Goto Github PK

View Code? Open in Web Editor NEW

This project forked from indexzero/ps-tree

0.0 4.0 2.0 49 KB

Fork of indexzero/ps-tree that also includes memory usage

Home Page: https://www.npmjs.com/package/@apify/ps-tree

License: MIT License

JavaScript 100.00%

ps-tree's Introduction

ps-tree

Build Status Code Climate Test Coverage npm Version Node.js Version Dependency Status

NOTE: This is a fork of ps-tree NPM package, with some fixes and extras.

Sometimes you cannot kill child processes like you would expect, this a feature of UNIX.

in UNIX, a process may terminate by using the exit call, and it's parent process may wait for that event by using the wait system call. the wait system call returns the process identifier of a terminated child, so that the parent tell which of the possibly many children has terminated. If the parent terminates, however, all it's children have assigned as their new parent the init process. Thus, the children still have a parent to collect their status and execution statistics. (from "operating system concepts")

Solution: use ps-tree to get all processes that a child_process may have started, so that they may all be terminated.

var cp = require('child_process'),
    psTree = require('ps-tree');

var child = cp.exec("node -e 'while (true);'", function () {...});

// This will not actually kill the child it will kill the `sh` process.
child.kill();

wtf? it's because exec actually works like this:

function exec (cmd, cb) {
  spawn('sh', ['-c', cmd]);
  ...
}

sh starts parses the command string and starts processes, and waits for them to terminate, but exec returns a process object with the pid of the sh process. However, since it is in wait mode killing it does not kill the children.

Use ps-tree like this:

var cp = require('child_process'),
    psTree = require('ps-tree');

var child = cp.exec("node -e 'while (true);'", function () { /*...*/ });

psTree(child.pid, function (err, children) {
  cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })));
});

If you prefer to run psTree from the command line, use: node ./bin/ps-tree.js

Cross Platform support

The ps-tree module behaves differently on *nix vs. Windows by spawning different programs and parsing their output. This is based on process.platform and not on checking to see if a ps compatible program exists on the system.

*nix

  1. " " need to be striped
$ ps -A -o comm,ppid,pid,stat
COMMAND          PPID   PID STAT
bbsd             2899 16958 Ss
watch <defunct>  1914 16964 Z
ps              20688 16965 R+

Windows

  1. wmic PROCESS WHERE ParentProcessId=4604 GET Name,ParentProcessId,ProcessId,Status)
  2. The order of head columns is fixed
> wmic PROCESS GET Name,ProcessId,ParentProcessId,Status
Name                          ParentProcessId  ProcessId   Status
System Idle Process           0                0
System                        0                4
smss.exe                      4                228

LICENSE: MIT

ps-tree's People

Contributors

bauerwer avatar dominictarr avatar eugenelim avatar fjakobs avatar huan avatar indexzero avatar jancurn avatar jbrumwell avatar kerphi avatar nelsonic avatar polikeiji avatar satvik007 avatar simonepri avatar

Watchers

 avatar  avatar  avatar  avatar

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.