How to run Node.js as a background process and never die?

前端 未结 14 942
刺人心
刺人心 2020-11-22 10:59

I connect to the linux server via putty SSH. I tried to run it as a background process like this:

$ node server.js &

However, after 2.5

相关标签:
14条回答
  • 2020-11-22 11:27

    another solution disown the job

    $ nohup node server.js &
    [1] 1711
    $ disown -h %1
    
    0 讨论(0)
  • 2020-11-22 11:27
    $ disown node server.js &
    

    It will remove command from active task list and send the command to background

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

    For Ubuntu i use this:

    (exec PROG_SH &> /dev/null &)

    regards

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

    Apart from cool solutions above I'd mention also about supervisord and monit tools which allow to start process, monitor its presence and start it if it died. With 'monit' you can also run some active checks like check if process responds for http request

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

    nohup will allow the program to continue even after the terminal dies. I have actually had situations where nohup prevents the SSH session from terminating correctly, so you should redirect input as well:

    $ nohup node server.js </dev/null &
    

    Depending on how nohup is configured, you may also need to redirect standard output and standard error to files.

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

    This is an old question, but is high ranked on Google. I almost can't believe on the highest voted answers, because running a node.js process inside a screen session, with the & or even with the nohup flag -- all of them -- are just workarounds.

    Specially the screen/tmux solution, which should really be considered an amateur solution. Screen and Tmux are not meant to keep processes running, but for multiplexing terminal sessions. It's fine, when you are running a script on your server and want to disconnect. But for a node.js server your don't want your process to be attached to a terminal session. This is too fragile. To keep things running you need to daemonize the process!

    There are plenty of good tools to do that.

    PM2: http://pm2.keymetrics.io/

    # basic usage
    $ npm install pm2 -g
    $ pm2 start server.js
    
    # you can even define how many processes you want in cluster mode:
    $ pm2 start server.js -i 4
    
    # you can start various processes, with complex startup settings
    # using an ecosystem.json file (with env variables, custom args, etc):
    $ pm2 start ecosystem.json
    

    One big advantage I see in favor of PM2 is that it can generate the system startup script to make the process persist between restarts:

    $ pm2 startup [platform]
    

    Where platform can be ubuntu|centos|redhat|gentoo|systemd|darwin|amazon.

    forever.js: https://github.com/foreverjs/forever

    # basic usage
    $ npm install forever -g
    $ forever start app.js
    
    # you can run from a json configuration as well, for
    # more complex environments or multi-apps
    $ forever start development.json
    

    Init scripts:

    I'm not go into detail about how to write a init script, because I'm not an expert in this subject and it'd be too long for this answer, but basically they are simple shell scripts, triggered by OS events. You can read more about this here

    Docker:

    Just run your server in a Docker container with -d option and, voilá, you have a daemonized node.js server!

    Here is a sample Dockerfile (from node.js official guide):

    FROM node:argon
    
    # Create app directory
    RUN mkdir -p /usr/src/app
    WORKDIR /usr/src/app
    
    # Install app dependencies
    COPY package.json /usr/src/app/
    RUN npm install
    
    # Bundle app source
    COPY . /usr/src/app
    
    EXPOSE 8080
    CMD [ "npm", "start" ]
    

    Then build your image and run your container:

    $ docker build -t <your username>/node-web-app .
    $ docker run -p 49160:8080 -d <your username>/node-web-app
    

    Hope this helps somebody landing on this page. Always use the proper tool for the job. It'll save you a lot of headaches and over hours!

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