9

I am starting a process from node with child_process.spawn and handling process.stdout/stderr data events, and writing to stdin.

Now, my node application may crash or get stopped, when it is restarted I find the process by it's PID, and then I would like to attach again to the process' stdin/stderr/stdout.

The process in question could be any daemon-like program, so I do not have control over it's behaviour (I cannot set up the process to redirect it's stdio upon receiving a signal, for example).

I am thinking wrapping the process using screen, redirecting stdio to a FIFO file (but the customFds option is deprecated in node 6), but none of that seems as clean as process.stdin.on...

Gipsy King
  • 1,549
  • 9
  • 15
  • 2
    Have you achive your goal? I will delete this comment after all but I was loking for something like that you asked, so share the answer If you had any success! – Leonardo Rick Aug 14 '20 at 19:14

1 Answers1

-1

There might be an easier way, using fuser I have created a situation here, where node.js has spawned a process and died

xxx@ubuntu:~/node$ node index.js 
Server has started
Request for / received.
About to route a request for /
Request handler 'start' was called

/home/xxx/node/requestHandlers.js:27
response.write(body);
            ^
ReferenceError: body is not defined
at Object.start (/home/xxx/node/requestHandlers.js:27:17)
at route (/home/xxx/node/node/router.js:4:18)
at Server.onRequest (/home/xxx/node/node/server.js:9:3)
at Server.emit (events.js:70:17)
at HTTPParser.onIncoming (http.js:1478:12)
at HTTPParser.onHeadersComplete (http.js:102:31)
at Socket.ondata (http.js:1374:22)
at TCP.onread (net.js:348:27)

If I run fuser <directory from which node started - example fuser /opt/node, I see the pids I created

xxxx@ubuntu:~$ fuser node
node:                16490c 16491

Just to be double sure - running ps, I can see the matching pids

xxxx@ubuntu:~$ ps -ef | grep find | grep -v grep
xxxx     16490     1  0 17:39 pts/0    00:00:00 /bin/sh -c find / -name 'moo'    
xxxx     16491 16490 21 17:39 pts/0    00:00:04 find / -name moo

I can run fuser -k /opt/node to kill and clean up the pids started from /opt/node. I personally use fuser regularly at work and home to clean up any leftover processes.

I have tested fuser on ubuntu and solaris.

NOTE: The only thing you need to be careful about is if there is an SSH session on that directory it will be nuked along with any other process started from that directory.

First Zero
  • 21,586
  • 6
  • 46
  • 45