npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webpod/ps

v0.0.0-beta.10

Published

A process lookup utility

Downloads

1,150

Readme

@webpod/ps

A Node.js module for looking up running processes. Originated from neekey/ps, UmbraEngineering/ps and completely reforged.

Differences

Install

$ npm install @webpod/ps

Internals

This module invokes different tools to get process list:

Usage

lookup()

Searches for the process by the specified pid.

import {lookup} from '@webpod/ps'

// Both callback and promise styles are supported
const list = await lookup({pid: 12345})

// or
lookup({pid: 12345}, (err, list) => {
  if (err) {
    throw new Error(err)
  }

  const [found] = list
  if (found) {
    console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', found.pid, found.command, found.arguments)
  } else {
    console.log('No such process found!')
  }
})

// or syncronously
const _list = lookup.sync({pid: 12345})

Define a query opts to filter the results by command and/or arguments predicates:

const list = await lookup({
  command: 'node', // it will be used to build a regex 
  arguments: '--debug',
})

list.forEach(entry => {
  console.log('PID: %s, COMMAND: %s, ARGUMENTS: %s', entry.pid, entry.command, entry.arguments);
})

Unix users can override the default ps arguments:

lookup({
  command: 'node',
  psargs: 'ux'
}, (err, resultList) => {
// ...
})

Specify the ppid option to filter the results by the parent process id (make sure that your custom psargs provides this output: -l or -j for instance)

lookup({
  command: 'mongod',
  psargs: '-l',
  ppid: 82292
}, (err, resultList) => {
 // ...
})

tree()

Returns a child processes list by the specified parent pid. Some kind of shortcut for lookup({ppid: pid}).

import { tree } from '@webpod/ps'

const children = await tree(123) 
/**
[
  {pid: 124, ppid: 123},
  {pid: 125, ppid: 123}
] 
*/

To obtain all nested children, set recursive option to true:

const children = await tree({pid: 123, recursive: true}) 
/**
[
  {pid: 124, ppid: 123},
  {pid: 125, ppid: 123},

  {pid: 126, ppid: 124},
  {pid: 127, ppid: 124},
  {pid: 128, ppid: 124},
  
  {pid: 129, ppid: 125},
  {pid: 130, ppid: 125},
] 
*/

// or syncronously
const list = tree.sync({pid: 123, recursive: true}) 

kill()

Eliminates the process by its pid.

import { kill } from '@webpod/ps'

kill('12345', (err, pid) => {
  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.

import { kill } from '@webpod/ps'

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

You can also use object notation to specify more opts:

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

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

License

MIT