如何在Node.js应用程序内部(以编程方式)重新启动应用程序?

28

我该如何创建一个可以自己重新启动的应用程序?我想要创建一个能够设置Web管理员并能够自动重启的应用程序。这种情况是否可行?如果可以,怎样实现呢?我认为这可能可以使用node内置的process全局变量来实现。


https://github.com/joyent/node/wiki/modules#wiki-build-and-deployment 上有几个模块声称可以轻松实现这一点。 - sarnold
8个回答

37

LK"I

不需要外部依赖,就可以实现:

console.log("This is pid " + process.pid);
setTimeout(function () {
    process.on("exit", function () {
        require("child_process").spawn(process.argv.shift(), process.argv, {
            cwd: process.cwd(),
            detached : true,
            stdio: "inherit"
        });
    });
    process.exit();
}, 5000);

来源: https://gist.github.com/silverwind/d0802f7a919ae86ff25e


是的,它在我的本地运行良好,但在Docker容器上无法运行。如果您能给我一些建议,那就太好了。 - Zhong Ri
它能工作,但是……如何停止它呢? 换句话说,如果我想要停止这个进程因为我需要改变脚本中的一些东西,我该怎么做呢?看起来当重新启动时,会忽略对现有脚本所做的更改! - Power Engineering

16

@Lord:特别注意restartrestartall命令。 - sarnold
哎呀,这些在自述文件里面没有提到。我现在正在尝试安装它,但是在Windows上遇到了一些问题。https://dev59.com/22DVa4cB1Zd3GeqPfaaJ - LordZardeck

14

我知道回复有点晚了,但我也有类似的需求。我想在进行配置更改时重新启动我的节点进程。我使用pm2来管理我的节点进程,所以这个过程非常简单。

在进行配置更改后,从节点进程内部执行process.exit()。据我所见,进程会退出,然后pm2会成功重新启动该进程。

目前还不确定是否有任何副作用,但它对我来说似乎运行良好。


5

0

我知道这个问题有点旧了,但它可能会帮助到其他人: 我建议使用nodeJS Cluster & Worker来实现这个目的!

const cluster = require('cluster');

if (cluster.isMaster ?? cluster.isPrimary) {
    cluster.fork();
    cluster.on("exit", (worker, code) => {
        console.log(`[master] worker #${worker.id} down, restarting\n`);
        cluster.fork();
    });
    process.on("SIGINT", () => { });
} else {
    console.log(`\nnew worker #${cluster.worker.id}\n`);
    process.on("SIGINT", () => {
        console.log(`[worker#${cluster.worker.id}] SIGINT received! dying gracefully!!\n`);
        process.exit(0);
    });
}

尝试使用nodejs运行它,并按下ctrl+c组合键。

它将重新启动,运行新代码。
您可以通过发送除SIGINT以外的任何信号来终止主进程。


0

虽然与该过程本身无关,但对于来到这里的人可能会很有用。

我将其添加到Linux的systemd中,并在network.target上启动,将控制台日志记录到/var/logs/<app>.log,并在失败时重新启动。因此,您只需在其中强制执行process.exit即可。

sudo touch /lib/systemd/system/<app>.service
sudo tee -a /lib/systemd/system/<app>.service > /dev/null <<EOT
[Unit]
Description=<app>
After=network.target
[Service]
Type=simple
User=$USER
ExecStart=/bin/node --prefix /home/$USER/path/to/app
Restart=on-failure # or always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=<app>
[Install]
WantedBy=multi-user.target
EOT

sudo touch /etc/rsyslog.d/<app>.conf
sudo tee -a /etc/rsyslog.d/<app>.conf > /dev/null <<EOT
if $programname == '<app>' then /var/log/<app>.log
& stop
EOT

sudo systemctl enable <app>
sudo systemctl daemon-reload
sudo systemctl restart rsyslog
sudo systemctl start <app>

-2
创建文件 nodemon.sh
#!/usr/bin/env bash
while true; do
            sleep 20
            echo "// nodemon $(date)" >config.js
        done

权限 sudo chmod +x nodemon.sh

运行它

./nodemon.sh


-3

是的,Upstart可以在没有nodemon的情况下重新启动您的进程。

npm install -g nodemon
sudo nodemon server.js

nodemon 会监视 nodemon 启动时所在的目录中的文件,如果这些文件发生更改,它将自动重新启动您的node应用程序。


我认为原帖作者并不想让他们的服务永远重新启动。相反,他们似乎想在特定条件下触发重启,或者如果这些条件未满足则退出。 - aggregate1166877

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接