ps-list-6.3.0/000077500000000000000000000000001345012564600131215ustar00rootroot00000000000000ps-list-6.3.0/.editorconfig000066400000000000000000000002571345012564600156020ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 ps-list-6.3.0/.gitattributes000066400000000000000000000000231345012564600160070ustar00rootroot00000000000000* text=auto eol=lf ps-list-6.3.0/.gitignore000066400000000000000000000000271345012564600151100ustar00rootroot00000000000000node_modules yarn.lock ps-list-6.3.0/.npmrc000066400000000000000000000000231345012564600142340ustar00rootroot00000000000000package-lock=false ps-list-6.3.0/.travis.yml000066400000000000000000000001061345012564600152270ustar00rootroot00000000000000os: - linux - windows language: node_js node_js: - '10' - '8' ps-list-6.3.0/index.d.ts000066400000000000000000000023201345012564600150170ustar00rootroot00000000000000declare namespace psList { interface Options { /** Include other users' processes as well as your own. On Windows this has no effect and will always be the users' own processes. @default true */ readonly all?: boolean; } interface ProcessDescriptor { readonly pid: number; readonly name: string; readonly ppid: number; /** Not supported on Windows. */ readonly cmd?: string; /** Not supported on Windows. */ readonly cpu?: number; /** Not supported on Windows. */ readonly memory?: number; /** Not supported on Windows. */ readonly uid?: number; } } declare const psList: { /** Get running processes. @returns List of running processes. @example ``` import psList = require('ps-list'); (async () => { console.log(await psList()); //=> [{pid: 3213, name: 'node', cmd: 'node test.js', ppid: 1, uid: 501, cpu: 0.1, memory: 1.5}, …] })(); ``` */ (options?: psList.Options): Promise; // TODO: remove this in the next major version, refactor the whole definition to: // declare function psList(options?: psList.Options): Promise; // export = psList; default: typeof psList; }; export = psList; ps-list-6.3.0/index.js000066400000000000000000000035251345012564600145730ustar00rootroot00000000000000'use strict'; const util = require('util'); const path = require('path'); const childProcess = require('child_process'); const TEN_MEGABYTES = 1000 * 1000 * 10; const execFile = util.promisify(childProcess.execFile); const windows = async () => { // Source: https://github.com/MarkTiedemann/fastlist const bin = path.join(__dirname, 'fastlist.exe'); const {stdout} = await execFile(bin, {maxBuffer: TEN_MEGABYTES}); return stdout .trim() .split('\r\n') .map(line => line.split('\t')) .map(([name, pid, ppid]) => ({ name, pid: Number.parseInt(pid, 10), ppid: Number.parseInt(ppid, 10) })); }; const main = async (options = {}) => { const flags = (options.all === false ? '' : 'a') + 'wwxo'; const ret = {}; await Promise.all(['comm', 'args', 'ppid', 'uid', '%cpu', '%mem'].map(async cmd => { const {stdout} = await execFile('ps', [flags, `pid,${cmd}`], {maxBuffer: TEN_MEGABYTES}); for (let line of stdout.trim().split('\n').slice(1)) { line = line.trim(); const [pid] = line.split(' ', 1); const val = line.slice(pid.length + 1).trim(); if (ret[pid] === undefined) { ret[pid] = {}; } ret[pid][cmd] = val; } })); // Filter out inconsistencies as there might be race // issues due to differences in `ps` between the spawns return Object.entries(ret) .filter(([, value]) => value.comm && value.args && value.ppid && value.uid && value['%cpu'] && value['%mem']) .map(([key, value]) => ({ pid: Number.parseInt(key, 10), name: path.basename(value.comm), cmd: value.args, ppid: Number.parseInt(value.ppid, 10), uid: Number.parseInt(value.uid, 10), cpu: Number.parseFloat(value['%cpu']), memory: Number.parseFloat(value['%mem']) })); }; module.exports = process.platform === 'win32' ? windows : main; // TODO: remove this in the next major version module.exports.default = module.exports; ps-list-6.3.0/index.test-d.ts000066400000000000000000000007611345012564600160030ustar00rootroot00000000000000import {expectType} from 'tsd'; import psList = require('.'); import {ProcessDescriptor} from '.'; const processes: ProcessDescriptor[] = await psList(); psList({all: false}); expectType(processes[0].pid); expectType(processes[0].name); expectType(processes[0].ppid); expectType(processes[0].cmd); expectType(processes[0].cpu); expectType(processes[0].memory); expectType(processes[0].uid); ps-list-6.3.0/license000066400000000000000000000021251345012564600144660ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ps-list-6.3.0/package.json000066400000000000000000000011031345012564600154020ustar00rootroot00000000000000{ "name": "ps-list", "version": "6.3.0", "description": "Get running processes", "license": "MIT", "repository": "sindresorhus/ps-list", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts", "fastlist.exe" ], "keywords": [ "ps", "proc", "process", "processes", "list", "running", "tasklist" ], "devDependencies": { "ava": "^1.4.1", "tsd": "^0.7.1", "xo": "^0.24.0" } } ps-list-6.3.0/readme.md000066400000000000000000000015731345012564600147060ustar00rootroot00000000000000# ps-list [![Build Status](https://travis-ci.org/sindresorhus/ps-list.svg?branch=master)](https://travis-ci.org/sindresorhus/ps-list) > Get running processes Works on macOS, Linux, and Windows. ## Install ``` $ npm install ps-list ``` ## Usage ```js const psList = require('ps-list'); (async () => { console.log(await psList()); //=> [{pid: 3213, name: 'node', cmd: 'node test.js', ppid: 1, uid: 501, cpu: 0.1, memory: 1.5}, …] })(); ``` > The `cmd`, `cpu`, `memory`, and `uid` properties are not supported on Windows. ## API ### psList([options]) Returns a `Promise` with the running processes. #### options Type: `Object` ##### all Type: `boolean`
Default: `true` Include other users' processes as well as your own. On Windows this has no effect and will always be the users' own processes. ## License MIT © [Sindre Sorhus](https://sindresorhus.com) ps-list-6.3.0/test.js000066400000000000000000000011071345012564600144350ustar00rootroot00000000000000import test from 'ava'; import psList from '.'; const isWindows = process.platform === 'win32'; test('main', async t => { const binName = isWindows ? 'node.exe' : 'ava'; const list = await psList(); t.true(list.some(x => x.name.includes(binName))); t.true( list.every(x => typeof x.pid === 'number' && typeof x.name === 'string' && typeof x.ppid === 'number' ) ); if (!isWindows) { t.true( list.every(x => typeof x.cmd === 'string' && typeof x.cpu === 'number' && typeof x.memory === 'number' && typeof x.uid === 'number' ) ); } });