获取正在运行的进程 stdin/stdout 与 node.js

发布于 2024-12-16 01:38:37 字数 459 浏览 0 评论 0 原文

我正在使用 child_process.spawn 从节点启动一个进程处理 process.stdout/stderr 数据事件,并写入 stdin。

现在,我的节点应用程序可能会崩溃或停止,当它重新启动时,我通过它的 PID 找到该进程,然后我想再次附加到该进程的 stdin/stderr/stdout。

有问题的进程可以是任何类似守护进程的程序,因此我无法控制它的行为(例如,我无法设置进程在接收到信号时重定向它的 stdio)。

我正在考虑使用 screen 包装进程,将 stdio 重定向到 FIFO 文件(但在节点 6 中不推荐使用 customFds 选项),但这些看起来都不像 process.stdin.on 那样干净...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2024-12-23 01:38:37

可能有一种更简单的方法,使用fuser我在这里创建了一种情况,其中node.js生成了一个进程并死亡

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)

如果我运行fuser - 示例 fuser /opt/node,我看到了我创建的 pid

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

只是为了双重确定 - 运行 ps,我可以看到

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

我可以运行的匹配 pid fusion-k /opt/node杀死并清理从 /opt/node 开始的 pid。我个人经常在工作和家里使用 fuser 来清理任何剩余的进程。

我已经在 ubuntu 和Solaris 上测试了fuser

注意:您唯一需要注意的是,如果该目录上有 SSH 会话,它将与从该目录启动的任何其他进程一起被破坏。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文