{"name":"terminate","version":"2.2.0","keywords":["Terminate","Kill","Node","Child","Process"],"author":{"url":"https://github.com/nelsonic","name":"@nelsonic","email":"contact.nelsonic@gmail.com"},"license":"GPL-2.0","_id":"terminate@2.2.0","homepage":"https://github.com/dwyl/terminate","bugs":{"url":"https://github.com/dwyl/terminate/issues"},"dist":{"shasum":"f6c03b798038b824210876f7e03508a99fe10f5b","tarball":"http://registry-npm.ywwl.com/terminate/download/terminate-2.2.0.tgz","fileCount":12,"integrity":"sha512-g7+BSZ7A70S+4nuFEp3cUdrr1yK4rkEDLOoKsdwfea5UOWxrpFqxaJoIlrGMcvDiEftuM6rh6bd+lw6+rmJQuQ==","signatures":[{"sig":"MEUCIQCYdlEoC44JfcoYIrjP/x3MT3mIKuKgsdgzKd33dq+XogIgDGd4zdGPlELcvxWGNqzXNgpZ299HEHhemnSHX8775dg=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":37177,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgnnuBCRA9TVsSAnZWagAAqS8P/R0/4cIqZxdCoS6eRAFB\nhGpqky8/QWWQQqY6l174JMvKaU3Kd/nno/Y/3rlUy54qzZB6X95q9ZbIdI3Z\nn97QiPHQ+I4MYyWnE4igD+faDGwSi/c68rXaExCN0YhoMWiRa3pLUeRLv/D9\nQtaf9nFOoLKdFu3q4pdQMpleVE7bJKYTSiAZYZdxlSUgGxpi6jo69Qaka8e8\ne61FZt61qgn5NINggcFDyDUQEmwE2ZpMcZXwHJ8VXdvQDB9MO4aSlci+M5w8\nyGBb5MJbaiBoibIdPq28U1QUbI1+ynOhm6x0F+YzXSZxGeVCyPRauNt25dSn\n1BZD0KLPmFgG2yIZNVketkgq3lJ34YqD3WDT7tiPidXTbFSfJXBMby8nQaSg\nS4C4ozN9wUxlEq0fjkl2uKzFUs9aH4NzG2ivgar30M5covwh6VMUb1cvew5p\nDGkWWanuwhSBf/iorRv5tRGHMQxweyXqqRkeg4oYm/iTBwBZdPjFTPk+PtdZ\nzR7YhpDfIeOWZS2O3WddyCV69IL2ilU8VBYnjitaO2WTOrpMdGrbzjgNidqW\nOqIVX9ksjDQvHqJKvj+TSsdzVAk8JvsBdYJ4HwatSB042ZOk0BkczarYjWZm\n+byNn0tmZ43US0aaaHDRcKRZvWK8+4KzIcI0/28NGMEmgrE7SZ5G4NGbRbEI\nNCQY\r\n=wfM3\r\n-----END PGP SIGNATURE-----\r\n","size":12670,"noattachment":false,"key":"/terminate/-/terminate-2.2.0.tgz"},"main":"terminate.js","engines":{"node":">=0.10"},"gitHead":"c6030569aa2d9e649f4975c316a5ad35734f961e","scripts":{"lint":"goodparts terminate.js","test":"istanbul cover --report lcov --report text ./node_modules/tape/bin/tape ./test/*.js","jshint":"./node_modules/jshint/bin/jshint -c .jshintrc --exclude-path .gitignore .","coverage":"npm test && istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100"},"_npmUser":{"name":"nelsonic","email":"contact.nelsonic@gmail.com"},"pre-commit":["jshint","coverage"],"repository":{"url":"git+https://github.com/dwyl/terminate.git","type":"git"},"_npmVersion":"6.14.11","description":"Terminate a Node.js Process based on the Process ID","directories":{},"_nodeVersion":"14.15.5","dependencies":{"ps-tree":"^1.2.0"},"_hasShrinkwrap":false,"devDependencies":{"tape":"^4.4.0","chalk":"^1.1.1","jshint":"^2.9.6","istanbul":"^0.4.5","goodparts":"1.2.1","pre-commit":"1.1.2"},"_npmOperationalInternal":{"tmp":"tmp/terminate_2.2.0_1620999041012_0.11788258039738353","host":"s3://npm-registry-packages"},"readme":"<div align=\"center\">\n\n# `terminate`\n\nA minimalist yet *reliable* way to **Terminate** a **`Node.js Process`** (and ***all Child Processes***) by **Process ID**.\n\n![terminate-the-node-process-final](https://cloud.githubusercontent.com/assets/194400/6859420/a3b63f3c-d410-11e4-91bb-ad6b607cc465.png)\n\n![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/dwyl/terminate/ci.yml?label=build&style=flat-square&branch=main)\n[![codecov.io](https://img.shields.io/codecov/c/github/dwyl/terminate/main.svg?style=flat-square)](http://codecov.io/github/dwyl/terminate?branch=main)\n[![npm version](https://badge.fury.io/js/terminate.svg?style=flat-square)](https://badge.fury.io/js/terminate)\n[![Node.js Version](https://img.shields.io/node/v/terminate.svg?style=flat-square)](https://nodejs.org/download)\n[![JavaScript Style Guide: Good Parts](https://img.shields.io/badge/code%20style-goodparts-brightgreen.svg?style=flat-square)](https://github.com/dwyl/goodparts \"JavaScript The Good Parts\")\n[![HitCount](http://hits.dwyl.com/dwyl/terminate.svg)](http://hits.dwyl.com/dwyl/terminate)\n[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat-square)](https://github.com/dwyl/hapi-auth-jwt2/issues)\n\n</div>\n\n## Usage\n\n### Install from NPM\n\n```sh\nnpm install terminate --save\n```\n\n### In your script\n\n```js\nconst terminate = require('terminate');\n\nterminate(process.pid, function (err) {\n  if (err) { // you will get an error if you did not supply a valid process.pid\n    console.log('Oopsy:', err); // handle errors in your preferred way.\n  }\n  else {\n    console.log('done'); // terminating the Processes succeeded.\n    // NOTE: The above won't be run in this example as the process itself will be killed before.\n  }\n});\n```\n\n### Usage\n\nAll the available parameters and their descriptions are viewable in the TypeScript definition file: [**`index.d.ts`**](./index.d.ts#L71).\n\n```ts\n// Function signature\nterminate(pid: number, signal?: string, opts?: TerminateOptions, callback?: DoneCallback): void\n\n// Function signature of the Promise version\nterminate(pid: number, signal?: string, opts?: TerminateOptions): Promise<void>\n```\n\n- `pid` - The Process ID you want to terminate.\n- `signal` - The signal to kill the processes with. Defaults to `\"SIGKILL\"` if it's empty or not defined.\n- `opts.pollInterval` - Interval to poll whether `pid` and all of the childs pids have been killed. Defaults to `500` (0.5s).\n- `opts.timeout` - Max time (in milliseconds) to wait for process to exit before timing out and calling back with an error. Defaults to `5000` (5s).\n- `callback` - The callback function to call when the termination is done. It has this signature: `(error: Error | null): void` whereas `error` will be `null` if the operation succeeds.\n\n### Promise API\n\nIf you are **using Node.js 8+**, you can load the Promise version importing the module `terminate/promise.js` like this:\n\n```js\nconst terminate = require('terminate/promise');\n\n(async () => {\n  try {\n    await terminate(process.pid);\n    console.log('done');\n  } catch (err) {\n    console.log('Oopsy:', err);\n  }\n})();\n```\n\n<br />\n\n# tl;dr\n\n## Why?\n\nIn our [***Faster***](https://github.com/ideaq/faster) project\nwe run the server using [**Child Process**](https://nodejs.org/api/child_process.html)(es).\nWhen we want to re-start the server,  \nwe needed to ensure the **process** (and any **Child Processes**)\nwere **Terminate**d before attempting to re-start.\n\n\n## *Prevents* Zombie Processes\n\nImagine you are running your Node.js app on a Multi-Core Processor\nand don't want to \"waste\" the CPU by only using *one* of those cores  \n(remember: one of Node's *selling points* is that its [single-process running on a single-core](https://stackoverflow.com/questions/17959663/)),\nyou can \"farm out\" tasks to the other cores using [**Child Process**](https://nodejs.org/api/child_process.html)(es)\n\nIf you then want to restart your app you *naively* (we did this!)\nterminate the \"main\" process and *expect* all the \"child\" processes to\nbe ended too.\n\nSadly this results in [***Zombie Processes***](https://stackoverflow.com/questions/27381163)\nwhich you would have to either *manually* kill or restart the machine/VM to clear.  \nInstead you need to *find* all the Process IDs for the child processes\nthat your app created and **terminate** those *before* you can re-start your app.\n\nUsing ***terminate*** you no longer have this problem.\n\n<br />\n\n## Dependencies [![Retire Status](https://img.shields.io/badge/security-no%20known%20vulnerabilities-brightgreen.svg?style=flat-square)](https://retire.insecurity.today/api/image?uri=https://raw.githubusercontent.com/dwyl/terminate/main/package.json)\n\nWhile researching how to get a list of child processes given a\nProcess ID we found **ps-tree**: https://github.com/indexzero/ps-tree  \nit was *un-maintained* and had *no tests*\n  so we submitted an [***issue***](https://github.com/indexzero/terminate/issues/10)\n  offering to update the module *with tests*.  \n  [*Charlie*](https://github.com/indexzero/terminate/issues/10#issuecomment-86795133)\n  replied welcoming an update so we submitted\n  a [***Pull Request***](https://github.com/indexzero/ps-tree/pull/12)\n  with ***100% test coverage*** which was [***Merged*** by *Charlie*](https://github.com/indexzero/ps-tree/pull/12#issuecomment-91785753)  \nps-tree in turn uses [**event-stream**](https://github.com/dominictarr/event-stream)\n(by [Dominc Tarr](https://github.com/dominictarr)) for streaming the result\nof its process lookups.  \n**event-stream** does not publish its' code coverage,\nbut is tested (and *more importantly* ***used*** by millions of people)  \nhas had ***many itterations*** and has been ***stable*** for a while:\n\n[![NPM](https://nodei.co/npm/event-stream.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/event-stream/)\n\nWhile we don't *like* the fact that **event-stream** has dependencies which have\n*limited* ***automated tests***,  \nwe are willing to place *reliance* on the module given how\n\"***real-world tested***\" it is.\n\n*If* at any point we find that relying **event-stream** or its underlying\ndependencies is the source of a bug or sleepless nights,  \nwe can either [***make time***](https://github.com/ideaq/time)\nto contribute the tests ***or*** *write our own* version of **ps-tree**\n*without dependencies*  \n(*which should \"only\" take a day but won't be as elegant...*)\n\n\n## Research\n\n### Background Reading\n\n+ How to terminate node processes: https://github.com/joyent/node/issues/1172 (history lesson)\n+ nodejs exec command failing with no useful error message:\nhttps://github.com/joyent/node/issues/4590\n\n### Useful StackOverflow Questions/Answers\n\n+ [spawning node.js child processes results in zombie processes on cloud foundry](https://stackoverflow.com/questions/27381163)\n+ [Why does my node app process keep getting stopped when I use forever?](https://stackoverflow.com/questions/26694100)\n+ [kill all child_process when node process is killed](https://stackoverflow.com/questions/18275809)\n+ [Why doesn't my Node.js process terminate once all listeners have been removed?](https://stackoverflow.com/questions/26004519)\n+ [nodejs exec command failing with no useful error message](https://stackoverflow.com/questions/14319724)\n+ [How many child processes can a node.js cluster spawn on a 64bit Wintel PC?](https://stackoverflow.com/questions/9275654)\n+ [Why is Node.js single threaded?](https://stackoverflow.com/questions/17959663)\n\n\nOther potential modules we could have used:\n\n+ **node-ps**: https://github.com/neekey/ps (has basic tests but no coverage / unmaintained?)\n+ **tree-kill**: https://www.npmjs.com/package/tree-kill (no tests!!)\n+ **nexpect**: https://github.com/nodejitsu/nexpect (spawn and control child processes\n  looks really good, but we wanted *less abstraction*)\n\n\n### Name\n\n[**Terminate**](https://www.google.co.uk/search?q=terminate)\nseemed like the *best* (of the available) name,  \nif you have a *better suggestion*, please share!\n\n<br />\n<br />\n\n## This Project Reminded me of Two *xkcd* Comics:\n\n![xkcd terminate](https://i.imgur.com/KQ9v7ll.png)\n\n![xkcd time robot](https://imgs.xkcd.com/comics/time_robot.png)\n","publish_time":1620999041205,"maintainers":[{"name":"nelsonic","email":"contact.nelsonic@gmail.com"}],"_cnpm_publish_time":1620999041205,"dist-tags":{"latest":"2.8.0"}}