Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Node.js service automatically on CENTOS 6.7?

Tags:

node.js

I have two servers running nodejs applications. I did some setting with the first one before. After that setting, when I start the command line, if I run node command, I see the service is running.

But I do not remember what I did. So in my second server, anytime I restart command line session, when I type node I get -bash: node: command not found.

Could anyone remind me please?

NOTE: Please don't tell me this is duplicate. Search for keywords "start, node service, automatically, etc." most of them tell about the use of 'forever'. I know forever (gdi), mine is a lot more stupid question and I don't know the correct terminology just yet.

like image 509
B.Y Avatar asked Feb 29 '16 16:02

B.Y


People also ask

How do I start node server automatically?

I would recommend installing your node. js app as a Windows service, and then set the service to run at startup. That should make it a bit easier to control the startup action by using the Windows Services snapin rather than having to add or remove batch files in the Startup folder.


1 Answers

I would suggest using pm2 or forever

For pm2 do the following Install it using

npm install pm2 -g

-g installs it globally. Then do following

pm2 start app.js --name="api" 

Once that is done, you can do pm2 list to view all running services as follows

pm2 list

Make pm2 start at boot time

pm2 startup

This will automatically start your node.js app.

Works for my 4 apps that are in production.

Hope this helps.

like image 172
Dave Amit Avatar answered Oct 21 '22 03:10

Dave Amit