使用cluster,setuid / setgid对子进程进行daemonizing node.js?

时间:2014-07-02 08:16:57

标签: node.js ubuntu cluster-computing daemon

当守护用于子进程的nodejs应用程序时,cluster.fork()中是否有一个选项允许设置用户:子组?对于主进程,我将用户和组设置为非root用户:

kue.app.listen(kueport, function() {
    process.setgid("uocoder");
    process.setuid("uocoder");
});

分叉子进程:

// FORK over all available cores
var clusterWorkerSize = require('os').cpus().length;  // Get number of cores
console.log("Forking over all " + clusterWorkerSize  + " cores");

for (var i = 0; i < clusterWorkerSize; i++) {
    cluster.fork();
}

当使用init.d script作为守护程序运行时,子进程以root用户身份运行,而主进程使用我在代码中设置的uid和gid。

$ ps aux | grep uocoder
uocoder   1484 20.5  0.6  42348 23632 ?        Sl   07:59   0:00 /usr/bin/nodejs /home/uocoder/uocoder/app.js
root      1487 21.5  0.5  40040 20812 ?        Sl   07:59   0:00 /usr/bin/nodejs /home/uocoder/uocoder/app.js
root      1489 21.0  0.5  40040 20808 ?        Sl   07:59   0:00 /usr/bin/nodejs /home/uocoder/uocoder/app.js

我还没有找到一种方法来设置cluster docs中子进程的uid和gid,也许我忽略了一些东西?

1 个答案:

答案 0 :(得分:0)

创建了由pull 7882修复的github问题:

https://github.com/joyent/node/issues/7881