summaryrefslogtreecommitdiffstats
path: root/node_modules/pidtree/lib/ps.js
diff options
context:
space:
mode:
authorGravatar Piotr Russ <mail@pruss.it> 2020-11-16 00:10:28 +0100
committerGravatar Piotr Russ <mail@pruss.it> 2020-11-16 00:10:28 +0100
commite06ec920f7a5d784e674c4c4b4e6d1da3dc7391d (patch)
tree55713f725f77b44ebfec86e4eec3ce33e71458ca /node_modules/pidtree/lib/ps.js
downloadwebsite_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.tar.gz
website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.tar.bz2
website_creator-e06ec920f7a5d784e674c4c4b4e6d1da3dc7391d.zip
api, login, auth
Diffstat (limited to 'node_modules/pidtree/lib/ps.js')
-rw-r--r--node_modules/pidtree/lib/ps.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/node_modules/pidtree/lib/ps.js b/node_modules/pidtree/lib/ps.js
new file mode 100644
index 0000000..6d9bb5f
--- /dev/null
+++ b/node_modules/pidtree/lib/ps.js
@@ -0,0 +1,47 @@
+'use strict';
+
+var os = require('os');
+var bin = require('./bin');
+
+/**
+ * Gets the list of all the pids of the system through the ps command.
+ * @param {Function} callback(err, list)
+ */
+function ps(callback) {
+ var args = ['-A', '-o', 'ppid,pid'];
+
+ bin('ps', args, function(err, stdout, code) {
+ if (err) return callback(err);
+ if (code !== 0) {
+ return callback(new Error('pidtree ps command exited with code ' + code));
+ }
+
+ // Example of stdout
+ //
+ // PPID PID
+ // 1 430
+ // 430 432
+ // 1 727
+ // 1 7166
+
+ try {
+ stdout = stdout.split(os.EOL);
+
+ var list = [];
+ for (var i = 1; i < stdout.length; i++) {
+ stdout[i] = stdout[i].trim();
+ if (!stdout[i]) continue;
+ stdout[i] = stdout[i].split(/\s+/);
+ stdout[i][0] = parseInt(stdout[i][0], 10); // PPID
+ stdout[i][1] = parseInt(stdout[i][1], 10); // PID
+ list.push(stdout[i]);
+ }
+
+ callback(null, list);
+ } catch (error) {
+ callback(error);
+ }
+ });
+}
+
+module.exports = ps;