├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 16 14 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Check if a process exists. 3 | 4 | @param input - The process ID or name to check. 5 | @returns Whether the process exists. 6 | */ 7 | export function processExists(input: number | string): Promise; 8 | 9 | /** 10 | Check multiple processes if they exist. 11 | 12 | @param input - The process IDs or names to check. 13 | @returns A map with the process name/ID as key and the status as a boolean value. 14 | */ 15 | export function processExistsMultiple( 16 | input: readonly T[], 17 | ): Promise>; 18 | 19 | /** 20 | Filter processes that exist. 21 | 22 | @param input - The process IDs or names to check. 23 | @returns The processes that exist. 24 | */ 25 | export function filterExistingProcesses>(input: T): T; 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import psList from 'ps-list'; 3 | 4 | const linuxProcessMatchesName = (wantedProcessName, process) => { 5 | if (typeof wantedProcessName === 'string') { 6 | return process.name === wantedProcessName || process.cmd.split(' ')[0] === wantedProcessName; 7 | } 8 | 9 | return process.pid === wantedProcessName; 10 | }; 11 | 12 | const nonLinuxProcessMatchesName = (wantedProcessName, process) => { 13 | if (typeof wantedProcessName === 'string') { 14 | return process.name === wantedProcessName; 15 | } 16 | 17 | return process.pid === wantedProcessName; 18 | }; 19 | 20 | const processMatchesName = process.platform === 'linux' ? linuxProcessMatchesName : nonLinuxProcessMatchesName; 21 | 22 | export async function processExists(processName) { 23 | const processes = await psList(); 24 | return processes.some(process_ => processMatchesName(processName, process_)); 25 | } 26 | 27 | export async function processExistsMultiple(processNames) { 28 | const processes = await psList(); 29 | return new Map(processNames.map(processName => [processName, processes.some(y => processMatchesName(processName, y))])); 30 | } 31 | 32 | export async function filterExistingProcesses(processNames) { 33 | const processes = await psList(); 34 | return processNames.filter(processName => processes.some(process_ => processMatchesName(processName, process_))); 35 | } 36 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import {processExists, processExistsMultiple, filterExistingProcesses} from './index.js'; 3 | 4 | expectType>(processExists(123)); 5 | expectType>(processExists('abc')); 6 | 7 | expectType>>( 8 | processExistsMultiple(['abc', 123]), 9 | ); 10 | 11 | expectType(filterExistingProcesses(['abc', 'foo'])); 12 | expectType>(filterExistingProcesses(['abc', 123])); 13 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "process-exists", 3 | "version": "5.0.0", 4 | "description": "Check if a process is running", 5 | "license": "MIT", 6 | "repository": "sindresorhus/process-exists", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "process", 27 | "processes", 28 | "proc", 29 | "ps", 30 | "pid", 31 | "test", 32 | "testing", 33 | "exists", 34 | "running", 35 | "check" 36 | ], 37 | "dependencies": { 38 | "ps-list": "^8.0.0" 39 | }, 40 | "devDependencies": { 41 | "ava": "^3.15.0", 42 | "noop-process": "^5.0.0", 43 | "tsd": "^0.18.0", 44 | "xo": "^0.46.4" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # process-exists 2 | 3 | > Check if a process is running 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install process-exists 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import {processExists, processExistsMultiple, filterExistingProcesses} from 'process-exists'; 15 | 16 | console.log(await processExists(process.pid)); 17 | //=> true 18 | 19 | const exists = await processExistsMultiple([process.pid, 'foo']); 20 | 21 | console.log(exists.get(process.pid)); 22 | //=> true 23 | 24 | console.log(exists.get('foo')); 25 | //=> false 26 | 27 | console.log(filterExistingProcesses(exists)); 28 | //=> [process.pid] 29 | ``` 30 | 31 | ## API 32 | 33 | ### processExists(input) 34 | 35 | Check if a process exists. 36 | 37 | Returns a `Promise`. 38 | 39 | #### input 40 | 41 | Type: `number | string` 42 | 43 | The process ID or name to check. 44 | 45 | ### processExistsMultiple(input) 46 | 47 | Check multiple processes if they exist. 48 | 49 | Returns a `Promise` with the process name/ID as key and the status as a boolean value. 50 | 51 | #### input 52 | 53 | Type: `Array` 54 | 55 | The process IDs or names to check. 56 | 57 | ### filterExistingProcesses(input) 58 | 59 | Filter processes that exist. 60 | 61 | Returns an `Array` with the processes that exist. 62 | 63 | #### input 64 | 65 | Type: `Array` 66 | 67 | The process IDs or names to check. 68 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import test from 'ava'; 3 | import noopProcess from 'noop-process'; 4 | import {processExists, processExistsMultiple, filterExistingProcesses} from './index.js'; 5 | 6 | test('pid', async t => { 7 | t.true(await processExists(process.pid)); 8 | t.false(await processExists(345_234_531)); 9 | }); 10 | 11 | test('title', async t => { 12 | const title = 'pe-test'; 13 | 14 | await noopProcess({title}); 15 | 16 | t.true(await processExists(title)); 17 | t.false(await processExists('pe-unicorn')); 18 | }); 19 | 20 | test('multiple', async t => { 21 | const title = 'pe-test'; 22 | await noopProcess({title}); 23 | 24 | t.deepEqual(await processExistsMultiple([process.pid, title, 345_234_531, 'pe-unicorn']), new Map([ 25 | [process.pid, true], 26 | [title, true], 27 | [345_234_531, false], 28 | ['pe-unicorn', false], 29 | ])); 30 | }); 31 | 32 | test('filter', async t => { 33 | const title = 'pe-test'; 34 | await noopProcess({title}); 35 | t.deepEqual(await filterExistingProcesses([process.pid, title, 345_234_531, 'pe-unicorn']), [process.pid, title]); 36 | }); 37 | --------------------------------------------------------------------------------