我正在使用 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...
发布评论
评论(1)
可能有一种更简单的方法,使用
fuser
我在这里创建了一种情况,其中node.js生成了一个进程并死亡如果我运行
fuser - 示例
fuser /opt/node
,我看到了我创建的 pid只是为了双重确定 - 运行
ps
,我可以看到我可以运行的匹配 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 diedIf I run
fuser <directory from which node started
- examplefuser /opt/node
, I see the pids I createdJust to be double sure - running
ps
, I can see the matching pidsI can run
fuser -k /opt/node
to kill and clean up the pids started from/opt/node
. I personally usefuser
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.