Reputation: 13
I use PM2 to monitor a NodeJS program, specifically a Discord bot. I recently upgraded my bot to use traditional sharding instead of internal sharding, meaning it splits the bot process up into multiple processes for better performance. The problem with this, is that my PM2 dashboard is now only monitoring the main process which starts the other processes, showing me a smaller RAM usage than what my program as a whole is using. I'm looking for a way to get PM2 to monitor all of the processes the main process creates, so I can get an accurate representation of how much CPU and RAM my program as a whole is using, allowing me to do things like automatic restart when the whole program reaches a certain amount of RAM.
How can I do this? I'm open to using alternative process management tools if this is not possible to do in PM2, but preferably, I would like to stick with PM2.
Upvotes: 1
Views: 403
Reputation: 153
PIDs can be found in shards, with this you can monitor the resources that the child process is using quite easily.
manager.on('shardCreate', (shard) => {
shard.on('spawn', (process) => {
console.log(process.pid)
})
});
Using pidusage, you should be easily able to track the resources each process is using.
var pidusage = require('pidusage')
pidusage(process.pid, function (err, stats) {
console.log(stats)
// => {
// 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
// }
cb()
})
Upvotes: 0