简体   繁体   中英

How to access the process object of child process [Node]

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
}
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM