2

I know I can run Node.js on server with command line node app.js.

But when I am out of control server, the session will be close and end my command. I don't know how to make a service run Node.js 24/7 like another in Server.

I follow this post, but I'm not using express.

ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110
xuan hung Nguyen
  • 390
  • 3
  • 12
  • Possible duplicate of [How do I run a node.js app as a background service?](https://stackoverflow.com/questions/4018154/how-do-i-run-a-node-js-app-as-a-background-service) – TGrif Sep 07 '17 at 15:06

2 Answers2

2

run it using forever , it helps the server to restart whenever the node server get crashed. https://www.npmjs.com/package/forever

Nilasis Sen
  • 299
  • 1
  • 10
  • this can be, because i connect to server on SSH, when i'm logout i will be close then. i need to know how to make simple service run on Centos 6.5. In this service run code to run NodeJS on Centos – xuan hung Nguyen Sep 07 '17 at 10:00
  • if you do forever start app.js and thn logout from ur ssh ur server will be still on and working . – Nilasis Sen Sep 07 '17 at 10:02
2

You can make it happen by many ways.

  1. You can append & in the command line to make the node server run in background.

    node app.js > stdout.txt > stderr.txt &
    
  2. Via process manager pm2, It gives more features, you can monitor all the processes pm2 monit, auto restart, etc

    npm install pm2 -g
    
    pm2 start app.js
    
  3. or, using following npm packages

Nodemon - nodemon app.js

Forever - forever start app.js.

karthick
  • 5,998
  • 12
  • 52
  • 90