Using Node.js is it possible to write to a process that has been detached?
I have a Node app that runs a process via child_process
and then detaches. But later on if I wanted to send a command to that process how would that be achieved with child_process
or any other package? Any information would be great thanks.
You can set an event handler for process.on('message')
to receive messages from the master
in the child_process
script, and receive messages from the child_process
instance by listening to the child.on('message')
event.
Example:
child.js
process.on('message', function(message) {
process.send('Hey you sent ' + m);
});
master.js
var fork = require('child_process').fork;
var worker = fork('./worker.js');
child.on('message', function(m) {
console.log('Recieved: ' + m);
});
// we could send as many messages as we want, the child process will
// will idle between messages
child.send('Message 1');
// Whenever you're done you can kill the child process
child.kill() // Default signal is `SIGTERM`
Via the child_process
API this is not possible.
If you are willing to use IPC and/or Unix domain sockets, consider this SO post: sending commands to a process in the background .
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.