So basically I am making a process class so I can spawn custom processes with ease later on. I want to access the process
object of the child process however I cannot do that for some reason. My code:
let cp = require("child_process")
class Process {
constructor(path, args) {
this.spawnedProcess = cp.fork(path, args)
}
getMemoryUsage() {
return this.spawnedProcess.memoryUsage() //errors here
}
}
TypeError: this.spawnedProcess.memoryUsage is not a function
How can i access the process object of a forked child process?
EDIT:
I have also tried the following
this.spawnedProcess.process.memoryUsage()
(errors with saying process is undefined)
As answered in how to get a child process memory usage in node.js?, here is a way to it using pidusage
.
let pidusage = require('pidusage');
const cp = require("child_process");
const child = cp.spawn('ls', ['-lh', '/usr']);
pidusage(child.pid, function (err, stats) {
console.log(stats);
});
/*
Output:
{
cpu: 10.0, // percentage (from 0 to 100*vcore)
memory: 357306368, // bytes
ppid: 312, // PPID
pid: 727, // PID
ctime: 867000, // ms user + system time
elapsed: 6650000, // ms since the start of the process
timestamp: 864000000 // ms since epoch
}
*/