在Ubuntu上使用Node.js和child_process生成的隐藏进程

时间:2018-08-30 10:08:33

标签: node.js process spawn

我有一个NodeJS应用程序,该应用程序连接到不同的传感器,并询问它们的当前值。为此,我有一个主进程,该进程使用MySQL监视当前传感器,并为每个活动的传感器生成一个子进程。子进程无限期运行。它每15秒获取一次传感器的URL,并将结果写入Redis DB。 因此,通常应该有一个主进程,子进程的数量应等于传感器的数量。但是我注意到,在htop中,我可以看到同一传感器运行着多个子进程。更奇怪的是,ps axf仅为每个传感器报告一个子进程。 这是ps axf的示例输出:

28079 ?        Ss     0:00  \_ sshd: zolcsi [priv]
28157 ?        S      0:00  |   \_ sshd: zolcsi@pts/8
28158 pts/8    Ss     0:00  |       \_ -bash
28173 pts/8    S      0:00  |           \_ sudo su
28175 pts/8    S      0:00  |               \_ su
28176 pts/8    S      0:00  |                   \_ bash
20424 pts/8    Sl+    0:00  |                       \_ node erzekeloLekerdezes.js
20630 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 4 http://***.net/****
20631 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 5 http://***.net/****
20632 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 7 http://***.net/****
20633 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 9 http://***.net/****
20634 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 11 http://***.net/****
20635 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 12 http://***.net/****
20636 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 13 http://***.net/****
20637 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 14 http://***.net/****
20638 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 17 http://***.net/****
20639 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 18 http://***.net/****
20640 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 19 http://***.net/****
20641 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 20 http://***.net/****
20642 pts/8    Sl+    0:00  |                           \_ node erzekeloLekerdezesChild.js 21 http://***.net/****

文件名后的数字是传感器的ID。这是htop的部分输出:

  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command 
20788 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
20791 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
20794 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
20797 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
21379 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
21717 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
21718 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
21719 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
21720 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.00 node erzekeloLekerdezesChild.js 4 http://***.net/****
20630 root       20   0 1160M 20048  5504 S  0.0  0.2  0:00.36 node erzekeloLekerdezesChild.js 4 http://***.net/****

如您所见,第4个传感器有1​​0个进程。这是一个很大的问题,因为有250个传感器,并且这些节点进程目前正在使用大量内存。 那么对其他9个过程(PID 20630除外)有什么想法?

这是主流程的相关代码:

const { spawn } = require('child_process');

let children = {};

function erzekelokLekerdezes() {
    let con = mysql.createConnection({
        host: config.database.host,
        user: config.database.user,
        password: config.database.pass,
        database: config.database.name
    });

    con.connect(function(err) {
        con.query(
            "SELECT " +
            "FROM " +
              "`LoxoneErzekelo` ",
            function (err, result) {
                for(let i = 0; i < result.length; i++) {
                    if(!result[i].Elo && children.hasOwnProperty(result[i].LoxoneErzekeloId)) {
                        //Sensor is off, but it has a running process, kill it
                        children[result[i].LoxoneErzekeloId].kill();
                    }
                    else if(result[i].Elo && !children.hasOwnProperty(result[i].LoxoneErzekeloId)) {
                        //Sensor is ON, and there is no child process for it, spawn one

                        let url = genUrl();
                        children[result[i].LoxoneErzekeloId] = spawn('node', [
                            'erzekeloLekerdezesChild.js',
                            result[i].LoxoneErzekeloId,
                            url,
                            result[i].Path
                        ]);
                        children[result[i].LoxoneErzekeloId].on('exit', function () {
                            delete children[result[i].LoxoneErzekeloId];
                        });
                    }
                }
                //Re-check the sensors after one minute
                setTimeout(erzekelokLekerdezes, 60000);
            }
        );
    });
}

erzekelokLekerdezes();

0 个答案:

没有答案
相关问题