2

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.

Datsik
  • 14,453
  • 14
  • 80
  • 121

2 Answers2

1

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.

Community
  • 1
  • 1
jonathan3692bf
  • 1,398
  • 1
  • 12
  • 14
  • In that case, how do you feel about Unix domain sockets and IPC? – jonathan3692bf Jun 03 '15 at 06:27
  • Anything that points me in the right direction would be fantastic – Datsik Jun 03 '15 at 06:28
  • 1
    Check this out. It will require you to change how you spawn your process, and likely forego child_process. http://stackoverflow.com/questions/5998126/send-command-to-a-background-process – jonathan3692bf Jun 03 '15 at 06:32
  • Move your comment to answer, this is one of the best things I've ever seen I'm fairly new to Linux but not so much Node so manipulating the OS is new to me. Thanks so much been beating my head off the wall forever. – Datsik Jun 03 '15 at 07:28
  • Only one quick question though, how do I start a long term process using fifo? it seems to hang when I do `echo "./srcds_run -game csgo -console -usercon +game_type 0 +game_mode 0 +mapgroup mg_active +map de_dust2"` – Datsik Jun 03 '15 at 07:38
  • Maybe this might help you? http://superuser.com/questions/502875/linux-named-sockets-howto – jonathan3692bf Jun 03 '15 at 07:49
  • Hmm I understand how the sockets and what not work but it doesn't really let me write to the process through the sockets, I can't exactly create the process as a socket can I? The fifo doesn't seem to work when I do: http://pastebin.com/M1Y57ZEi it just echos back the command but never starts the server, and same with the sockets – Datsik Jun 03 '15 at 08:48
0

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`
undefined
  • 2,154
  • 15
  • 16
  • Thanks for the reply. The problem Im facing is that the program is started detached so I need to be able to reattach to it later if I need to like send s command to its process ID or something. – Datsik Jun 03 '15 at 04:31
  • Ah I see, Ya am not quite sure how one would go about that, at least with the `child_process` api. – undefined Jun 03 '15 at 04:34
  • No problem, sorry I couldn't be of more help. – undefined Jun 03 '15 at 04:37