• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ps-tree

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

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

Mac/Darwin

  1. " " need to be striped
$ ps -A -o comm,ppid,pid,stat
COMM              PPID   PID STAT
/sbin/launchd        0     1 Ss
/usr/libexec/Use     1    43 Ss

LICENSE: MIT

More Repositories

1

nconf

Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.
JavaScript
3,866
star
2

broadway

Lightweight App extensibility and hookable middleware customization.
JavaScript
250
star
3

node-pkginfo

An easy way to expose properties on a module from a package.json
JavaScript
160
star
4

nodejs-intro

My introduction presentation to node.js along with sample code at various stages of building a simple RESTful web service with director, cradle, winston, optimist, and http-console.
JavaScript
128
star
5

node-schema-org

A node.js library that retrieves, parses and provides all schemas from schema.org
JavaScript
105
star
6

http-agent

A simple agent for performing a sequence of http requests in node.js
JavaScript
92
star
7

errs

Simple error creation and passing utilities
JavaScript
74
star
8

roadmap

A simple CLI script to generate a formatted Roadmap from Github issues and milestones
JavaScript
55
star
9

node-asana-api

A node.js client implementation for Asana API.
JavaScript
41
star
10

fashion-show

Build consistent and versioned styleguides by including and running consistent lint files across projects.
JavaScript
26
star
11

nconf-redis

A redis store for nconf
JavaScript
26
star
12

populist-style

A (one day) auto-updating style at the mercy of the people, and only the people.
JavaScript
22
star
13

presentations

Various presentations that I have made over the span of my time on this pale blue dot.
JavaScript
20
star
14

window-stream

A collection of streams for Windowing events
JavaScript
18
star
15

uuid-time

Get time from RFC4122 uuids
JavaScript
13
star
16

license-tree

Read a tree of files and check for licenses.
JavaScript
12
star
17

dotfiles

Seemed like the thing do to. My dotfiles.
Shell
12
star
18

morgan-json

A variant of `morgan.compile` that provides format functions that output JSON
JavaScript
12
star
19

node-procfile

Procfile parser for node.js
JavaScript
10
star
20

month-ends

Purposefully simple and dependency free functions for calendar months
JavaScript
10
star
21

twitter-no-backend

A simple example of how to use Twitter without any backend.
CSS
10
star
22

picasso

Feather weight CLI tool for synching Github issue labels across repos.
JavaScript
10
star
23

changes

A consistent, fault tolerant CouchDB _changes listener with pre-fetch support
JavaScript
9
star
24

npm-static-stats

Comprehensive static analysis for npm packages through esprima
JavaScript
8
star
25

npm-pipeline

Analysis pipeline for npm packages
JavaScript
8
star
26

xlsx-rows

Parses an *.xlsx file into rows
JavaScript
8
star
27

wpf-window-chrome

A set of controls for using custom window chrome in WPF
C#
7
star
28

node-checkout

Pull down local or remote repositories to local directories.
JavaScript
7
star
29

git-semver2-tag

Upgrades any semver 1.0 tags to semver 2.0. e.g. "v1.0.0" becomes "1.0.0"
JavaScript
6
star
30

exceptiony

A simple remote logging service written in node.js
JavaScript
6
star
31

baltar

A few small utilities for working with tarballs and http.
JavaScript
6
star
32

celluloid

Celluloid is a reactive, input-output oriented language with support for blocks and timelines designed to make both simple and complex media composition operations more comprehensible to the author.
Java
6
star
33

footage

Get your Open Source game footage up to speed.
JavaScript
5
star
34

conver

Comparisons for concrete semantic versions (i.e. compare 1.2.3 instead of ^1.2.3)
JavaScript
5
star
35

git-lint

Lint gitconfig files for fun and profit
JavaScript
5
star
36

node-objs

A complete set of utilities for working with Objects.
JavaScript
5
star
37

fgnpmr

For those special times when replicating npm just won't cut it
JavaScript
4
star
38

npm-publish-split-stream

Splits an npm publish request using jsonstream and duplexify
JavaScript
4
star
39

postcss-global-scss-vars

Inject global variables into your scss using postcss
JavaScript
4
star
40

npm-verify-stream

A duplex stream for receiving a package tarball, verifying arbitrary checks, and emitting that same tarball on success.
JavaScript
4
star
41

hungry-birds

CLI tool for dumping various twitter API results to JSON file(s).
JavaScript
4
star
42

node-artsy

A Node.js client for the Artsy API
JavaScript
3
star
43

npmcount

Silly program that counts number of npm packages from one or more users.
JavaScript
3
star
44

vbump

A very simple version bumping script I've been using for years.
Shell
3
star
45

d3.hexhex

Make a hexagon out of other hexagons in d3
JavaScript
3
star
46

quasar

A Silverlight Control Library
C#
3
star
47

postcss-strip-font-face

PostCSS plugin to completely strip @font-face rules from css. In other words: koh the @font-faceย stealer.
JavaScript
3
star
48

bruce-wayne

A collection of projects and samples written as Bruce Wayne (read: .NET Developer)
C#
3
star
49

dotnet-tools

A set of macros, snippets, and other files that can be really useful for .NET development
Visual Basic
3
star
50

prism-insertable-regions

Extends Prism v2 to allow IRegion to support the Insert operation
C#
2
star
51

designer-mvvm-toolkit

A small set of Model-View-ViewModel tools for implementing Designer Friendly MVVM applications in WPF and Silverlight
C#
2
star
52

npm-codependencies

Calculate codependency relationships for npm modules.
JavaScript
2
star
53

isla.js

A Javascript runtime environment for the Isla language
JavaScript
2
star
54

tar-buffer

Buffers entries from a tar.Parse() stream into memory
JavaScript
2
star
55

npm-comp-stat-www

A set of statistical visualizations for npm packages
JavaScript
2
star
56

zoho-inventory

Fetch-based API client for the Zoho Inventory API
JavaScript
2
star
57

nerf-gun

A tiny utility that returns nerf darts from URLs for npm.
JavaScript
2
star
58

jsonm

A complete json parser written using MGrammar and C#
C#
2
star
59

es6wtf

It's like wtfjs, but for ES6 only
2
star
60

wildcodes

My solutions to http://www.trywildcard.com/challenge
JavaScript
2
star
61

read-ssl

A simple node library to read SSL certificate files according to a basic convention.
JavaScript
2
star
62

surface-raw-input

A collection of behaviors for capturing and working with raw input on the Microsoft Surface
C#
2
star
63

homebase-parser

Parser & file reader for the ABE Homebase 2.0 text (*.txt) format.
JavaScript
1
star
64

gifs

They move. They're funny. They're protected under DMCA.
1
star
65

radiant-scribbish-theme

A port of the scribbish theme to RadiantCMS
JavaScript
1
star
66

ux-logging-toolkit

A toolkit of WPF behaviors and services for logging UX events in a sessions aware manner.
C#
1
star
67

js-lint-compat

Comparison of options for various linting tools. Currently eshint, jshint, and jscs.
1
star
68

npm-package-buffer

Buffers entries from a tar.Parse() stream for an npm package into memory
JavaScript
1
star
69

npmignore-tests

Tells you what modules in `node_modules` npmignore tests
JavaScript
1
star
70

voicebox-www

A bump UI for VoiceBox karaoke
CSS
1
star
71

node-clf-parser

A basic parser for the common log format seen in apache and nginx logs
JavaScript
1
star
72

shared-mocha-suite

An interface to define shareable, configurable mocha tests for use in multiple mocha executions
1
star
73

tap-flattener

Flattens tap output including subtests into a single level hierarchy
JavaScript
1
star