Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

find-process

yibn20085.7mMIT1.4.10TypeScript support: included

find process info by port/pid/name etc.

node, process, pid, port

readme

find-process

Node.js CI js-standard-style

With find-process, you can:

  • find the process which is listening specified port
  • find the process by pid
  • find the process by given name or name pattern

We have covered the difference of main OS platform, including Mac OSX, Linux, Windows and Android (with Termux).

CLI

Install find-process as a CLI tool:

$ npm install find-process -g

Usage:


  Usage: find-process [options] <keyword>


  Options:

    -V, --version      output the version number
    -t, --type <type>  find process by keyword type (pid|port|name)
    -p, --port         find process by port
    -h, --help         output usage information

  Examples:

    $ find-process node          # find by name "node"
    $ find-process 111           # find by pid "111"
    $ find-process -p 80         # find by port "80"
    $ find-process -t port 80    # find by port "80"

Example:

image

Node API

You can use npm to install:

$ npm install find-process --save

Usage:

const find = require('find-process');

find('pid', 12345)
  .then(function (list) {
    console.log(list);
  }, function (err) {
    console.log(err.stack || err);
  })

Synopsis

Promise<Array> find(type, value, [options])

Arguments

  • type the type of find, support: port|pid|name
  • value the value of type, can be RegExp if type is name
  • options this can either be the object described below or boolean to just set strict mode
    • options.strict the optional strict mode is for checking port, pid, or name exactly matches the given one. (on Windows, .exe can be omitted)
    • options.logLevel set the logging level to trace|debug|info|warn|error. In practice this lets you silence a netstat warning on Linux.

Return

The return value of find-process is Promise, if you use co you can use yield find(type, value) directly.

The resolved value of promise is an array list of process ([] means it may be missing on some platforms):

[{
  pid: <process id>,
  ppid: [parent process id],
  uid: [user id (for *nix)],
  gid: [user group id (for *nix)],
  name: <command/process name>,
  bin: <execute path (for *nix)>,
  cmd: <full command with args>
}, ...]

Example

Find process which is listening port 80.

const find = require('find-process');

find('port', 80)
  .then(function (list) {
    if (!list.length) {
      console.log('port 80 is free now');
    } else {
      console.log('%s is listening port 80', list[0].name);
    }
  })

Find process by pid.

const find = require('find-process');

find('pid', 12345)
  .then(function (list) {
    console.log(list);
  }, function (err) {
    console.log(err.stack || err);
  });

Find all nginx process.

const find = require('find-process');

find('name', 'nginx', true)
  .then(function (list) {
    console.log('there are %s nginx process(es)', list.length);
  });

Find all nginx processes on Linux without logging a warning when run as a user who isn't root.

const find = require('find-process');

find('name', 'nginx', {strict: true, logLevel: 'error'})
  .then(function (list) {
    console.log('there are %s nginx process(es)', list.length);
  });

Contributing

We're welcome to receive Pull Request of bugfix or new feature, but please check the list before sending PR:

  • Coding Style Please follow the Standard Style
  • Documentation Add documentation for every API change
  • Unit test Please add unit test for bugfix or new feature

License

MIT

changelog

1.4.7 / 2021-11-18

  • chore: bump to 1.4.7
  • fix: fix undefined issue #40
  • fix: fix github actions
  • fix: fix install method

1.4.6 / 2021-11-18

  • chore: bump to 1.4.6
  • fix: fix number check issue

1.4.5 / 2021-09-21

  • chore: bump to 1.4.5
  • fix: fix find-process
  • chore(deps): bump path-parse from 1.0.6 to 1.0.7
  • chore(deps): bump y18n from 4.0.0 to 4.0.1

1.4.4 / 2020-10-22

  • chore: bump to 1.4.4
  • chore(deps): bump lodash from 4.17.15 to 4.17.20
  • fix: Security updates, Windows unit tests

1.4.3 / 2019-11-15

  • chore: bump to 1.4.3
  • fix: fix #30

1.4.2 / 2019-06-14

  • chore: bump to 1.4.2

1.4.1 / 2019-03-22

  • chore: bump to 1.4.1
  • fix: fix issue #9

1.4.0 / 2019-03-22

  • chore: bump to 1.4.0
  • feat: throw error when run on *nix system

1.3.0 / 2019-03-22

  • chore: bump to 1.3.0
  • feat: support executable path
  • docs: add bin prop to get execute path

1.2.3 / 2019-03-22

  • chore: bump to 1.2.3

1.2.2 / 2019-03-22

  • fix: fix name issue
  • chore: bump to 1.2.2

1.2.1 / 2018-11-15

  • chore: add changelog

1.2.0 / 2018-10-19

  • fix some bugs
  • support android platform
  • add strict mode when finding by name
  • for pid, ppid, uid, gid, always return number
  • add official type declaration.

1.1.4 / 2018-10-19

  • chore: bump to 1.1.4

1.1.3 / 2018-08-20

  • chore: bump to 1.1.3

1.1.2 / 2018-08-14

  • chore: bump to 1.1.2

1.1.1 / 2018-04-16

  • fix: add ignore config
  • chore: bump to 1.1.1

1.1.0 / 2017-07-14

  • chore: bump to 1.1.0
  • feat: add cli bin support
  • fix: fix issue #2

1.0.5 / 2016-10-13

  • fix: incorrect error info

1.0.4 / 2016-03-20

  • fix #1

1.0.3 / 2016-02-04

  • Use standard coding style
  • Remove unused deps

1.0.2 / 2016-02-02

  • Add gitignore keyword
  • Add prepublish hook
  • Fix: Error occured when pid not exists

1.0.1 / 2016-01-24

  • Initial version, support find process by port/pid/name