提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.5 KiB

  1. <h1 align="center">
  2. <b>pidtree</b>
  3. </h1>
  4. <p align="center">
  5. <!-- Version - npm -->
  6. <a href="https://www.npmjs.com/package/pidtree">
  7. <img src="https://img.shields.io/npm/v/pidtree.svg" alt="Latest version on npm" />
  8. </a>
  9. <!-- Downloads - npm -->
  10. <a href="https://npm-stat.com/charts.html?package=pidtree">
  11. <img src="https://img.shields.io/npm/dt/pidtree.svg" alt="Downloads on npm" />
  12. </a>
  13. <!-- License - MIT -->
  14. <a href="https://github.com/simonepri/pidtree/tree/master/license">
  15. <img src="https://img.shields.io/github/license/simonepri/pidtree.svg" alt="Project license" />
  16. </a>
  17. <br/>
  18. <!-- Lint -->
  19. <a href="https://github.com/simonepri/pidtree/actions?query=workflow:lint+branch:master">
  20. <img src="https://github.com/simonepri/pidtree/workflows/lint/badge.svg?branch=master" alt="Lint status" />
  21. </a>
  22. <!-- Test - macOS -->
  23. <a href="https://github.com/simonepri/pidtree/actions?query=workflow:test-macos+branch:master">
  24. <img src="https://github.com/simonepri/pidtree/workflows/test-macos/badge.svg?branch=master" alt="Test macOS status" />
  25. </a>
  26. <!-- Test - Ubuntu -->
  27. <a href="https://github.com/simonepri/pidtree/actions?query=workflow:test-ubuntu+branch:master">
  28. <img src="https://github.com/simonepri/pidtree/workflows/test-ubuntu/badge.svg?branch=master" alt="Test Ubuntu status" />
  29. </a>
  30. <!-- Test - Windows -->
  31. <a href="https://github.com/simonepri/pidtree/actions?query=workflow:test-windows+branch:master">
  32. <img src="https://github.com/simonepri/pidtree/workflows/test-windows/badge.svg?branch=master" alt="Test Windows status" />
  33. </a>
  34. <!-- Coverage - Codecov -->
  35. <a href="https://codecov.io/gh/simonepri/pidtree">
  36. <img src="https://img.shields.io/codecov/c/github/simonepri/pidtree/master.svg" alt="Codecov Coverage report" />
  37. </a>
  38. <!-- DM - Snyk -->
  39. <a href="https://snyk.io/test/github/simonepri/pidtree?targetFile=package.json">
  40. <img src="https://snyk.io/test/github/simonepri/pidtree/badge.svg?targetFile=package.json" alt="Known Vulnerabilities" />
  41. </a>
  42. <br/>
  43. <!-- Code Style - XO-Prettier -->
  44. <a href="https://github.com/xojs/xo">
  45. <img src="https://img.shields.io/badge/code_style-XO+Prettier-5ed9c7.svg" alt="XO Code Style used" />
  46. </a>
  47. <!-- Test Runner - AVA -->
  48. <a href="https://github.com/avajs/ava">
  49. <img src="https://img.shields.io/badge/test_runner-AVA-fb3170.svg" alt="AVA Test Runner used" />
  50. </a>
  51. <!-- Test Coverage - Istanbul -->
  52. <a href="https://github.com/istanbuljs/nyc">
  53. <img src="https://img.shields.io/badge/test_coverage-NYC-fec606.svg" alt="Istanbul Test Coverage used" />
  54. </a>
  55. <!-- Init - ni -->
  56. <a href="https://github.com/simonepri/ni">
  57. <img src="https://img.shields.io/badge/initialized_with-ni-e74c3c.svg" alt="NI Scaffolding System used" />
  58. </a>
  59. <!-- Release - np -->
  60. <a href="https://github.com/sindresorhus/np">
  61. <img src="https://img.shields.io/badge/released_with-np-6c8784.svg" alt="NP Release System used" />
  62. </a>
  63. </p>
  64. <p align="center">
  65. 🚸 Cross platform children list of a PID.
  66. <br/>
  67. <sub>
  68. Coded with ❤️ by <a href="#authors">Simone Primarosa</a>.
  69. </sub>
  70. </p>
  71. ## Synopsis
  72. This package is really similar to [ps-tree][gh:ps-tree] but is faster, safer and
  73. provides sub-children results.
  74. Furthermore ps-tree is [unmaintained][gh:ps-tree-um].
  75. Uuh, and a fancy [CLI](#cli) is also available!
  76. ## Usage
  77. ```js
  78. var pidtree = require('pidtree')
  79. // Get childs of current process
  80. pidtree(process.pid, function (err, pids) {
  81. console.log(pids)
  82. // => []
  83. })
  84. // Include the given pid in the result array
  85. pidtree(process.pid, {root: true}, function (err, pids) {
  86. console.log(pids)
  87. // => [727]
  88. })
  89. // Get all the processes of the System (-1 is a special value of this package)
  90. pidtree(-1, function (err, pids) {
  91. console.log(pids)
  92. // => [530, 42, ..., 41241]
  93. })
  94. // Include PPID in the results
  95. pidtree(1, {advanced: true}, function (err, pids) {
  96. console.log(pids)
  97. // => [{ppid: 1, pid: 530}, {ppid: 1, pid: 42}, ..., {ppid: 1, pid: 41241}]
  98. })
  99. // If no callback is given it returns a promise instead
  100. const pids = await pidtree(1)
  101. console.log(pids)
  102. // => [141, 42, ..., 15242]
  103. ```
  104. ## Compatibility
  105. | Linux | FreeBSD | NetBSD | SunOS | macOS | Win | AIX |
  106. | --- | --- | --- | --- | --- | --- | --- |
  107. | ✅ | ❓ | ❓ | ❓ | ✅ | ✅ | ❓ |
  108. ✅ = Working
  109. ❓ = Not tested but should work
  110. Please if your platform is not supported [file an issue][new issue].
  111. ## CLI
  112. <img src="https://github.com/simonepri/pidtree/raw/master/media/cli.gif" alt="pidtree cli" width="300" align="right"/>
  113. Show a tree of the processes inside your system inside your terminal.
  114. ```bash
  115. npx pidtree $PPID
  116. ```
  117. Just replace `$PPID` with one of the pids inside your system.
  118. Or don't pass anything if you want all the pids inside your system.
  119. ```bash
  120. npx pidtree
  121. ```
  122. To display the output as a list, similar to the one produced from `pgrep -P $PID`,
  123. pass the `--list` flag.
  124. ```bash
  125. npx pidtree --list
  126. ```
  127. ## API
  128. <a name="pidtree"></a>
  129. ## pidtree(pid, [options], [callback]) ⇒ <code>[Promise.&lt;Array.&lt;Object&gt;&gt;]</code>
  130. Get the list of children pids of the given pid.
  131. **Kind**: global function
  132. **Returns**: <code>Promise.&lt;Array.&lt;Object&gt;&gt;</code> - Only when the callback is not provided.
  133. **Access**: public
  134. | Param | Type | Default | Description |
  135. | --- | --- | --- | --- |
  136. | pid | <code>Number</code> \| <code>String</code> | | A pid. If -1 will return all the pids. |
  137. | [options] | <code>Object</code> | | Optional options object. |
  138. | [options.root] | <code>Boolean</code> | <code>false</code> | Include the provided pid in the list. Ignored if -1 is passed as pid. |
  139. | [callback] | <code>function</code> | | Called when the list is ready. If not provided a promise is returned instead. |
  140. ## Related
  141. - [pidusage][gh:pidusage] -
  142. Cross-platform process cpu % and memory usage of a PID
  143. ## Authors
  144. - **Simone Primarosa** - [simonepri][github:simonepri]
  145. See also the list of [contributors][contributors] who participated in this project.
  146. ## License
  147. This project is licensed under the MIT License - see the [license][license] file for details.
  148. <!-- Links -->
  149. [new issue]: https://github.com/simonepri/pidtree/issues/new
  150. [license]: https://github.com/simonepri/pidtree/tree/master/license
  151. [contributors]: https://github.com/simonepri/pidtree/contributors
  152. [github:simonepri]: https://github.com/simonepri
  153. [gh:pidusage]: https://github.com/soyuka/pidusage
  154. [gh:ps-tree]: https://github.com/indexzero/ps-tree
  155. [gh:ps-tree-um]: https://github.com/indexzero/ps-tree/issues/30