使用 node.js 获取正在运行的进程的 stdin/stdout [英] Get running process' stdin/stdout with node.js

查看:53
本文介绍了使用 node.js 获取正在运行的进程的 stdin/stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从节点开始一个进程 child_process.spawn 和处理 process.stdout/stderr 数据事件,并写入标准输入.

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

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

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.

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

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

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

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

推荐答案

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

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)

如果我运行 fuser <directory from which node started - 示例 fuser/opt/node,我会看到我创建的 pid

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

再次确认 - 运行 ps,我可以看到匹配的 pids

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

我可以运行 fuser -k/opt/node 来终止和清理从 /opt/node 开始的 pid.我个人经常在工作和家里使用 fuser 来清理任何剩余的进程.

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.

我已经在 ubuntu 和 solaris 上测试了 fuser.

I have tested fuser on ubuntu and solaris.

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

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.

这篇关于使用 node.js 获取正在运行的进程的 stdin/stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆