Get process stats with nodejs
js
Node.js
let child_process = require("child_process");
let PID = 6991;
child_process.exec("ps --pid " + PID + " -o %cpu,rss,vsz,%mem", (error, stdout, stderr) => {
// prepare data before send
let txt = stdout.replace(/\n$/i, "").split("\n"); // remove the last \n and split by \n
let lbltxt = txt[0].split(/\s+/g); // split by change space chars
let datatxt = txt[1].replace(/^\s+/g, "").split(/\s+/g); // remove the first space chars and split by space chars
console.log(stdout);
console.log(txt);
console.log(lbltxt);
console.log(datatxt);
let psinfo = {};
for (let i = 0; i < lbltxt.length; i++) {
console.log(i + " " + lbltxt[i] + " " + datatxt[i]);
psinfo[lbltxt[i]] = datatxt[i];
}
console.log(psinfo);
});
// infos