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

前端 未结 26 1048
隐瞒了意图╮
隐瞒了意图╮ 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 06:21

    Check out fugue! Apart from launching many workers, you can demonize your node process too!

    http://github.com/pgte/fugue

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

    This answer is quite late to the party, but I found that the best solution was to write a shell script that used the both the screen -dmS and nohup commands.

    screen -dmS newScreenName nohup node myserver.js >> logfile.log
    

    I also add the >> logfile bit on the end so I can easily save the node console.log() statements.

    Why did I use a shell script? Well I also added in an if statement that checked to see if the node myserver.js process was already running.

    That way I was able to create a single command line option that both lets me keep the server going and also restart it when I have made changes, which is very helpful for development.

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

    If you are using pm2, you can use it with autorestart set to false:

    $ pm2 ecosystem

    This will generate a sample ecosystem.config.js:

    module.exports = {
      apps: [
        {
          script: './scripts/companies.js',
          autorestart: false,
        },
        {
          script: './scripts/domains.js',
          autorestart: false,
        },
        {
          script: './scripts/technologies.js',
          autorestart: false,
        },
      ],
    }
    

    $ pm2 start ecosystem.config.js

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

    This might not be the accepted way, but I do it with screen, especially while in development because I can bring it back up and fool with it if necessary.

    screen
    node myserver.js
    >>CTRL-A then hit D
    

    The screen will detach and survive you logging off. Then you can get it back back doing screen -r. Hit up the screen manual for more details. You can name the screens and whatnot if you like.

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

    To round out the various options suggested, here is one more: the daemon command in GNU/Linux, which you can read about here: http://libslack.org/daemon/manpages/daemon.1.html. (apologies if this is already mentioned in one of the comments above).

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

    If you are running nodejs in linux server, I think this is the best way.

    Create a service script and copy to /etc/init/nodejs.conf

    start service: sudo service nodejs start

    stop service: sudo service nodejs stop

    Sevice script

    description "DManager node.js server - Last Update: 2012-08-06"
    author      "Pedro Muniz - pedro.muniz@geeklab.com.br"
    
    env USER="nodejs" #you have to create this user 
    env APPNAME="nodejs" #you can change the service name
    env WORKDIR="/home/<project-home-dir>" #set your project home folder here
    env COMMAND="/usr/bin/node <server name>" #app.js ?
    
    # used to be: start on startup
    # until we found some mounts weren't ready yet while booting:
    start on started mountall
    stop on shutdown
    
    # Automatically Respawn:
    respawn
    respawn limit 99 5
    
    pre-start script
        sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/$APPNAME.log
    end script
    
    script
        # Not sure why $HOME is needed, but we found that it is:
        export HOME="<project-home-dir>"  #set your project home folder here
        export NODE_PATH="<project node_path>"
    
        #log file, grant permission to nodejs user
        exec start-stop-daemon --start --make-pidfile --pidfile /var/run/$APPNAME.pid --chuid $USER --chdir $WORKDIR --exec $COMMAND >> /var/log/$APPNAME.log 2>&1
    end script
    
    post-start script
       # Optionally put a script here that will notifiy you node has (re)started
       # /root/bin/hoptoad.sh "node.js has started!"
    end script
    
    pre-stop script
        sudo -u $USER echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/$APPNAME.log
    end script
    
    0 讨论(0)
提交回复
热议问题