How does one start a node.js server as a daemon process?

后端 未结 8 1525
余生分开走
余生分开走 2020-11-30 17:41

In Python Twisted, you have the twistd command that helps you with a number of things related to running your application (daemonize it for example).

Ho

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 17:55

    If you need your process to daemonize itself, not relaying on forever - you can use the daemonize module.

    $ npm install daemonize2
    

    Then just write your server file as in example:

    var daemon = require("daemonize2").setup({
        main: "app.js",
        name: "sampleapp",
        pidfile: "sampleapp.pid"
    });
    
    switch (process.argv[2]) {
    
        case "start":
            daemon.start();
            break;
    
        case "stop":
            daemon.stop();
            break;
    
        default:
            console.log("Usage: [start|stop]");
    }
    

    Mind you, that's rather a low level approach.

提交回复
热议问题