1
我正試圖實現類似於當使用NodeJS啓動時的行爲httpd
。當我們說:'fork_ed'的守護進程'child_process'
service httpd start
在ps
,我們會看到:
[[email protected] ~]# service httpd start
Starting httpd: [ OK ]
[[email protected] ~]# ps auxf | grep httpd
root 3395 0.0 0.1 6336 304 pts/0 R+ 12:03 0:00 | \_ grep httpd
root 3391 0.0 1.3 175216 3656 ? Ss 12:02 0:00 /usr/sbin/httpd
apache 3393 0.0 0.9 175216 2432 ? S 12:02 0:00 \_ /usr/sbin/httpd
通知httpd
主機和子女怎麼沒有終端(?
顯示,而不是pts/0
爲grep
)。
現在...我需要一個IPC通道,因此我使用child_process.fork但無論我做什麼,每次看到一個終端仍然連接到我的守護進程。這裏是歡迎你做實驗的代碼:
c.js
- 控制器
var cp = require('child_process');
var d = cp.fork('d.js', {});
d.on('message', function() {
d.disconnect();
d.unref();
});
d.js
- 守護
process.send('ready');
setTimeout(function() {
console.log('test');
}, 10000);
這是我在終端看到:
[[email protected] ~]# node c.js # running the control script
[[email protected] ~]# ps auxf | grep node # my terminal is interactive again so I check
root 3472 0.0 0.3 103308 864 pts/0 S+ 12:13 0:00 | \_ grep node
root 3466 1.1 5.6 648548 14904 pts/0 Sl 12:13 0:00 /usr/bin/node d.js
[[email protected] ~]# test # appears outta nowhere because d.js still has this stdout
d.js
還有我的pts/0
並寫入test
,即使它已經與我交互bash
。
我該如何解決這個問題並讓守護進程終止?我不在乎哪一方(c.js
或d.js
)得到調整代碼,我同時控制,但我需要IPC通道,因此這必須通過fork
完成。
相關[問題](https://github.com/odin-public/aps-node-runtime/issues/7)。 – Gear54rus