Start node app through ssh, stays running? - node.js

I have a raspberry PI on which I run a node server. To start and control the terminal on which the server runs I use desktop remote to remote control the raspberry. Now this method is really slow so I was wondering, since I only need a command line anyway if I couldn't just connect to my raspberry pi using ssh for example.
My question now, would be if I do so, can I navigate to my node folder, run my node file and then close the ssh connection? Will my Node server keep running and if so how would I access the terminal with the node session after closing the connection?

The easiest way to do this is something like:
nohup node myapp.js &
This will make the app run in the background, and nohup prevents it from stopping when the connection closes.
This is a cheap and quick way to do this. A more appropriate way might be one of the following:
Using something like docker to manage running applications.
Using something like supervisord to do the same thing.
Writing scripts for initd and turn it into a real 'service'.
Changing the node application to fork & deamonize itself.

Related

automatically start node server on instance start in aws autoscaling by providing user data

I have a demo project in AWS and then I created an AMI for it so that I can use it for auto-scaling. now I am looking for something that I can put in user text in my launch configuration which will let me start the server without going to ssh. I am trying out below, let me know where is my mistake.
#!/bin/bash
cd demo
node server.js
when I launch a new instance with my AMI and just do cd through SSH it works absolutely fine, however, I want to start the server with going to SSH.
These are common one can face when running node application without process manager on a remote server.
Let suppose the above script but what if a node application encounter error? so the application will be stopped, so better to use process manager which will take care of such thing and you will not need to do ssh.
You can use pm2. Which also have slack integration another interesting feature that will help to monitor the process.
You can also set Setup startup script.
Restarting PM2 with the processes you manage on server boot/reboot is
critical. To solve this, just run this command to generate an active
startup script:
run these command in the AMI, and pm2 will take care of the process on all instances.
pm2 startup
#And to freeze a process list for automatic respawn:
pm2 save

Nodejs application session is not alive after closing putty terminal even after using nohup command in manage hosting server

I have an application running on a managed server. I deployed the application after many hurdles but now the problem is that the application process is killed once i close the putty terminal.
I was suggested to use the following command to run the process in background, but this command is not doing any help?
nohup npm start --production &
I have googled and people have suggested to use screen, but i want to avoid using it because installing new packages brings new challenges to my app (i am new to nodejs deployment)
My Question : What is the problem in nohup command ?
My nohup.out file :
Warning: connect.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale
past a single process. connected as id 2515208
OK so i used screen. It was pre installed on server , i just need to use it by calling a couple of commands.
Its easy to use and my application is working fine till now.
But still i want to know what nohup does ? because it was useless for me.

DigitalOcean stop Node.js server running with nohup

I am running a Node.js server on a DigitalOcean droplet (with Ubuntu). I have worked out how to make it run when I'm not connected to it via Putty. However, just one issue: how do I stop it now?
I can see that control+C works when in the session, but what if I exit the session and come back? How will I stop the server then?
Also, will running it multiple times run multiple servers at once?
Thanks!
You really should be using a tool like supervisord (http://supervisord.org/) for your long-running processes.
But if you want to stop an already running process that you started with nohup then look up the process ID first (with ps aux and look/grep for your process) and then run kill <<pid>>.

Starting a node server and leaving it running

This may be a stupid question, sorry.
But I SSH into a server and run my node server with the command:
node server.js
I note however that if my SSH sessions stops abruptly (internet cuts out etc) then I am confronted with an error. Network error unexpectedly closed session or something similar.
I then note that my node server has actually stopped. However nothing has actually gone wrong. It was as if I had Ctrl + C'd but otherwise I simply timed out from my SSH session.
So clearly I'm doing something wrong. Is there a way to ssh in, run the serve rand disconnect without the server turning itself off or similar? Or to avoid my SSH session collapsing and taking the server with it?
Assuming you are not on Windows, you can just start it without a console like this:
node server.js &
And, then it won't be wired into your ssh console.
Or, you can use something like forever to launch node as its own process and then monitor it and auto-restart it.
forever server.js
Lots more info here.

nodejs server run on remote

I wasn't quite sure what to call this question but here i go:
i have a remote server where i have installed node.js now normally this would be how i start the server:
ssh root#ip
cd /var/www/mydomain/server
nodejs server.js
This works without any issues however what happens when i close down the terminal? How can i make sure that the server doesn't just stop. And how can i control it after i have started it (for instance restarting / stopping it).
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.

Resources