Think of having an admin app which creates & manages child apps.
I m spawning another child_process ( using childProcess.spawn('node', 'somefile.js')
). And, I m able to set event listeners (for ex, exit
) on the childProcess, so that I can log the output & will know on child app exit.
But when the admin app restarts, I want to listen for the events on the child process again, so that adminapp will know the child app is crashed.
But child_process
module doesn't have any method to get the childProcess instance by 'pid' (or some other mechanism).
Can someone give pointers how to do this? Can we do this using forever-monitor or pm2-api?
Update: Code I've to spawn the child process:
let userWorkProcess = childProcess.spawn('node', [ 'someapp.js' ],
{
detached: true,
stdio: [ 'ignore', logFile, logFile ],
cwd: __dirname + '/someapp'
}
);
userWorkProcess.unref();
userWorkProcess.on('exit', function () {
if (userWorkProcess.exitCode !== 0) {
// child process exited with some error
} else {
// child process exited without any error
}
});
Update #2:
Can we connect to the process? For example, I see some code like that in forever-monitor. Is it possible using socket/nssocket?
https://github.com/foreverjs/forever/blob/master/lib/forever.js#L199 https://github.com/foreverjs/forever/blob/master/lib/forever.js#L229