我正在从节点启动一个进程child_process.spawn http://nodejs.org/docs/v0.6.1/api/child_processes.html#child_process.spawn处理 process.stdout/stderr 数据事件,并写入 stdin。
现在,我的节点应用程序可能会崩溃或停止,当它重新启动时,我通过它的 PID 找到该进程,然后我想再次附加到该进程的 stdin/stderr/stdout。
有问题的进程可以是任何类似守护进程的程序,因此我无法控制它的行为(例如,我无法设置进程在接收到信号时重定向它的 stdio)。
我正在考虑使用 screen 包装进程,将 stdio 重定向到 FIFO 文件(但在节点 6 中不推荐使用 customFds 选项),但这些似乎都不像 process.stdin.on 那样干净...
可能有一种更简单的方法,使用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 <directory from which node started
- 例子fuser /opt/node
,我看到了我创建的pid
xxxx@ubuntu:~$ fuser node
node: 16490c 16491
只是为了双重确定 - 跑步ps
,我可以看到匹配的pid
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
杀死并清理 pids 开始于/opt/node
。我个人使用fuser
定期在工作和家里清理任何剩余的流程。
我测试过fuser
在 ubuntu 和 solaris 上。
NOTE:您唯一需要注意的是,如果该目录上有 SSH 会话,它将与从该目录启动的任何其他进程一起被破坏。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)