How do I run a node.js app as a background service?

前端 未结 26 1046
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 05:28

Since this post has gotten a lot of attention over the years, I\'ve listed the top solutions per platform at the bottom of this post.


Original post

相关标签:
26条回答
  • 2020-11-22 05:59

    has anyone noticed a trivial mistaken of the position of "2>&1" ?

    2>&1 >> file
    

    should be

    >> file 2>&1
    
    0 讨论(0)
  • 2020-11-22 06:01

    I am surprised that nobody has mentioned Guvnor

    I have tried forever, pm2, etc. But, when it comes to solid control and web based performance metrics, I have found Guvnor to be by far the best. Plus, it is also fully opensource.

    Edit : However, I am not sure if it works on windows. I've only used it on linux.

    0 讨论(0)
  • 2020-11-22 06:01

    Since I'm missing this option in the list of provided answers I'd like to add an eligible option as of 2020: docker or any equivalent container platform. In addition to ensuring your application is working in a stable environment there are additional security benefits as well as improved portability.

    There is docker support for Windows, macOS and most/major Linux distributions. Installing docker on a supported platform is rather straight-forward and well-documented. Setting up a Node.js application is as simple as putting it in a container and running that container while making sure its being restarted after shutdown.

    Create Container Image

    Assuming your application is available in /home/me/my-app on that server, create a text file Dockerfile in folder /home/me/my-app with content similar to this one:

    FROM node:lts-alpine
    COPY /my-app /app
    CMD ["/app/server.js"]
    

    Create the image using command like this:

    docker build -t myapp-as-a-service /home/me
    

    Note: Last parameter is selecting folder containing that Dockerfile instead of the Dockerfile itself. You may pick a different one using option -f.

    Start Container

    Use this command for starting the container:

    docker run -d --restart always -p 80:3000 myapp-as-a-service
    

    This command is assuming your app is listening on port 3000 and you want it to be exposed on port 80 of your host.

    This is a very limited example for sure, but it's a good starting point.

    0 讨论(0)
  • 2020-11-22 06:02

    2016 Update: The node-windows/mac/linux series uses a common API across all operating systems, so it is absolutely a relevant solution. However; node-linux generates systemv init files. As systemd continues to grow in popularity, it is realistically a better option on Linux. PR's welcome if anyone wants to add systemd support to node-linux :-)

    Original Thread:

    This is a pretty old thread now, but node-windows provides another way to create background services on Windows. It is loosely based on the nssm concept of using an exe wrapper around your node script. However; it uses winsw.exe instead and provides a configurable node wrapper for more granular control over how the process starts/stops on failures. These processes are available like any other service:

    enter image description here

    The module also bakes in some event logging:

    enter image description here

    Daemonizing your script is accomplished through code. For example:

    var Service = require('node-windows').Service;
    
    // Create a new service object
    var svc = new Service({
      name:'Hello World',
      description: 'The nodejs.org example web server.',
      script: 'C:\\path\\to\\my\\node\\script.js'
    });
    
    // Listen for the "install" event, which indicates the
    // process is available as a service.
    svc.on('install',function(){
      svc.start();
    });
    
    // Listen for the "start" event and let us know when the
    // process has actually started working.
    svc.on('start',function(){
      console.log(svc.name+' started!\nVisit http://127.0.0.1:3000 to see it in action.');
    });
    
    // Install the script as a service.
    svc.install();
    

    The module supports things like capping restarts (so bad scripts don't hose your server) and growing time intervals between restarts.

    Since node-windows services run like any other, it is possible to manage/monitor the service with whatever software you already use.

    Finally, there are no make dependencies. In other words, a straightforward npm install -g node-windows will work. You don't need Visual Studio, .NET, or node-gyp magic to install this. Also, it's MIT and BSD licensed.

    In full disclosure, I'm the author of this module. It was designed to relieve the exact pain the OP experienced, but with tighter integration into the functionality the Operating System already provides. I hope future viewers with this same question find it useful.

    0 讨论(0)
  • 2020-11-22 06:03

    Try to run this command if you are using nohup -

    nohup npm start 2>/dev/null 1>/dev/null&
    

    You can also use forever to start server

    forever start -c "npm start" ./ 
    

    PM2 also supports npm start

    pm2 start npm -- start
    
    0 讨论(0)
  • 2020-11-22 06:05

    I use Supervisor for development. It just works. When ever you make changes to a .js file Supervisor automatically restarts your app with those changes loaded.

    Here's a link to its Github page

    Install :

    sudo npm install supervisor -g

    You can easily make it watch other extensions with -e. Another command I use often is -i to ignore certain folders.

    You can use nohup and supervisor to make your node app run in the background even after you log out.

    sudo nohup supervisor myapp.js &

    0 讨论(0)
提交回复
热议问题