Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a node.js application run permanently?

Tags:

node.js

server

People also ask

How do I make NodeJS run forever?

Installing forever Forever can be used in two ways: with the forever CLI tool and with forever-monitor. The forever-monitor tool can be used to run an application with forever using code.

How do I run NodeJS automatically?

Restart the application automatically The node command has to be re-executed in bash whenever there is a change in the application. To restart the application automatically, use the nodemon module. This local installation of nodemon can be run by calling it from within npm script such as npm start or using npx nodemon.


You could install forever using npm like this:

sudo npm install -g forever

And then start your application with:

forever server.js

Or as a service:

forever start server.js

Forever restarts your app when it crashes or stops for some reason. To restrict restarts to 5 you could use:

forever -m5 server.js

To list all running processes:

forever list

Note the integer in the brackets and use it as following to stop a process:

forever stop 0

Restarting a running process goes:

forever restart 0

If you're working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:

forever -w server.js

Although the other answers solve the OP's problem, they are all overkill and do not explain why he or she is experiencing this issue.

The key is this line, "I close putty, then I cannot reach the address"

When you are logged into your remote host on Putty you have started an SSH linux process and all commands typed from that SSH session will be executed as children of said process.

Your problem is that when you close Putty you are exiting the SSH session which kills that process and any active child processes. When you close putty you inadvertently kill your server because you ran it in the foreground. To avoid this behavior run the server in the background by appending & to your command:

node /srv/www/MyUserAccount/server/server.js &

The problem here is a lack of linux knowledge and not a question about node. For some more info check out: http://linuxconfig.org/understanding-foreground-and-background-linux-processes

UPDATE:

As others have mentioned, the node server may still die when exiting the terminal. A common gotcha I have come across is that even though the node process is running in bg, it's stdout and stderr is still pointed at the terminal. This means that if the node server writes to console.log or console.error it will receive a broken pipe error and crash. This can be avoided by piping the output of your process:

node /srv/www/MyUserAccount/server/server.js > stdout.txt 2> stderr.txt &

If the problem persists then you should look into things like tmux or nohup, which are still more robust than node specific solutions, because they can be used to run all types of processes (databases, logging services, other languages).

A common mistake that could cause the server to exit is that after running the nohup node your_path/server.js & you simply close the Putty terminal by a simple click. You should use exit command instead, then your node server will be up and running.


You can use PM2, it's a production process manager for Node.js applications with a built-in load balancer.

Install PM2

$ npm install pm2 -g

Start an application

$ pm2 start app.js

If you using express then you can start your app like

pm2 start ./bin/www --name="app"

Listing all running processes:

$ pm2 list

It will list all process. You can then stop / restart your service by using ID or Name of the app with following command.

$ pm2 stop all                  
$ pm2 stop 0                    
$ pm2 restart all               

To display logs

$ pm2 logs ['all'|app_name|app_id]

I'd recommend looking for something such as Forever to restart Node in the event of a crash, and handle daemonizing this for you.


If you just want to run your node app in the terminal always, just use screen.

Install on ubuntu/ debian:

sudo apt-get install screen

Usage:

$ screen
$ node /path/to/app.js

ctrl + a and then ctrl + d to dismiss

To get is back:

One screen: screen -r

If there's more than one you can list all the screens with: screen -ls

And then: screen -r pid_number


You could simply use this

nohup node /srv/www/MyUserAccount/server/server.js &

This will keep the application running and to shut it down you will have to kill it.

For that you could install htop and then search for node and then kill it


Forever is a very good NodeJs module to do exactly that.

Install forever by typing in the command line

$ npm install forever -g

Then use the following command to run a node.js script

$ forever start /path/to/script.js

You are good to go. Additionally you can run

$ forever list

to see all the running scripts. You can terminate any specific script by typing

$ forever stop [pid]

where [pid] is the process ID of the script you will obtain from the list command. To stop all scripts, you may type

$ forever stopall