作为系统服务运行时,NodeJS child_process.spawn()表现不同

时间:2020-01-26 20:28:27

标签: node.js typescript debian systemd

我正在开发一个NodeJS应用程序,该应用程序运行expressJS并使用闪烁方式拨打电话号码。

提供以下功能:

export const call = (telNr: string, res: Response|undefined = undefined) => {
    const endOfLine = require("os").EOL;
    const process = spawn("/usr/bin/twinkle", ["-c"], {shell: true});
    let registered = false;
    process.stdout.on("data", (data) => {
        if (/registration succeeded/.test(data.toString())) {
            if (!registered) {
                registered = true;
                process.stdin.write("call " + telNr + endOfLine);
                setTimeout(() => {
                    try {
                        process.stdin.write("bye" + endOfLine);
                        process.stdin.write("exit" + endOfLine);
                    } catch {
                        // War schon zu
                    }
                }, 10000);
            } // Else -> Scheint das zweite mal ausgegeben zu werden.
        }

        if (/486 Busy here/.test(data.toString())) {
            // Belegt
            process.stdin.write("exit" + endOfLine);
        }
    });

    process.on("exit", (exit) => {
        if (res !== undefined) {
            res.json({code: exit});
        }
    });
};

该函数用于通过url调用的调试目的(这就是为什么有可选的res参数的原因。

现在要解决问题

当我以node index.js作为root用户启动应用程序并打开给定URL时,所需的SIP电话会响10秒钟。

当应用通过systemd作为服务启动时,子进程立即以退出代码134关闭。

系统单位:

[Unit]
Description=Besuchermanagement Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/bin/node /srv/node/besuchermanagement/dist/index.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

我希望这不是题外话,因为我不确定这个问题是否与我的代码有关,或者与systemd中服务的配置错误有关。

谢谢!

0 个答案:

没有答案