How can I restart a Node.js app from within itself (programmatically)?

后端 未结 5 2312
余生分开走
余生分开走 2020-12-15 06:14

How can I create an app that can restart itself? I want to create an app that sets up a web-admin which can restart itself. Is this possible? If so, how? I was thinking this

5条回答
  •  清歌不尽
    2020-12-15 06:25

    LK"I

    It is possible without external dependencies:

    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);
    

    source : https://gist.github.com/silverwind/d0802f7a919ae86ff25e

提交回复
热议问题