How to Start a Nodejs Process on a Remote Server

How to start a NodeJS process on a remote server?

That's not a specific nodejs problem, although there are specific solutions (for example forever).

A generic solution to remotely start any program on linux and not have it die when you close the session is to launch it using nohup. Here's an example in which I launch node and redirect both the standard and error outputs into the server.log file :

nohup node main.js >> server.log 2>&1 < /dev/null & 

Spawn process on remote host in NodeJS

The second argument passed to spawn() is an array of arguments. Also, you don't need to manually quote your arguments. This should work:

spawn('ssh', ['pi@raspi2', 'ls'], { stdio: ['pipe', 'pipe', 2, 'pipe'] });

Lastly, if you want more programmatic (and lightweight) control over the ssh connection, there is the ssh2 module (or any of the modules that build on top of it), which does not use child processes.

nodejs server run on remote

There are plenty of solutions here, but maybe the most easy to start with is using forever.

Forever is a npm module that keep your app running and restarts it if it crashes.

Also there are more advanced solutions, like using PM2, which I recommend, but first take a look at forever.

nodejs execute command on remote linux server

I solved the problem myself. There is one npm package (ssh-exec) available for ssh command execution. Below is the code I used.

var exec = require('ssh-exec')
var v_host = 'XX.XX.XX.XXX'
exec('ls -lh', {
user: 'root',
host: 'XX.XX.XX.XXX',
password: 'password'
}).pipe(process.stdout , function (err, data) {
if ( err ) { console.log(v_host); console.log(err); }
console.log(data)
})

How to make a node.js application run permanently?

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.

How to connect a remote server from client by http.get of Node.js?

Did not set up the port for 3000 or 80 at the vary beginning.

How to run node.js app forever when console is closed?

You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.

Install upstart:

sudo apt-get install upstart

Create a simple script for your application that will look something like:

#!upstart
description "my app"

start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

env NODE_ENV=production

exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1

Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:

sudo start myapp
sudo stop myapp
sudo restart myapp


Related Topics



Leave a reply



Submit