Tal Avissar
Tal Avissar

Reputation: 10304

Sending message from child process to parent process when creating child_process with exec()

I have a parent process in Node.JS which creates a child process through calling exec. I want to wait until the child process finish and return the status of the child process.

I don't want to use spawn or fork.

I'm creating the child_process with require('child_process').exec

I need the child process to send message to the parent process through IPC:

function foo()
{    
    const exec = require('child_process').exec;

     const cmd = `cd /usr/lib/bin' && db-migrate --config "config/${environmentName}.json" -e ${environmentName} -v true up  --force-exit`;

    const child = exec(cmd, (error, stdout, stderr) => {
         //...
    });

    child.on('exit', (code) => {
       //from here i want to know if there was a problem in child process
       //can I use IPC to send messages?
    });
     //wants to return the child status code from here
 return child_status_code;
}

How can i solve this problem?

How can I use IPC from child process to parent process?

Upvotes: 1

Views: 1406

Answers (1)

RaphaelYu
RaphaelYu

Reputation: 47

If there is a IPC channel created in process, you are able to send message from child to parent by "process.send“. It is addressed in documentation of Nodejs. The IPC channel is only available by "fork". "fork" has the first parameter is module path.

Upvotes: -1

Related Questions