Npm start in background on a server after SSH connection (EC2) - node.js

I have an app that I start with "npm start"
I would like to run it in background.
This app is hosted on aws EC2.
I tried :
npm start &
Doesnt work if I disconnect ssh connection
nohup npm start &
Same, it fail if I disconnect ssh connection
I tried to launch with root, or user, doesnt make any difference.
I also tried forever but I didnt manage to launch with npm start like "forever start npm start" doesnt work
Thanks a lot for your help

You have to use docker.
You will need to read a lot the documentation, because it's complicated to understand if you dont know it : https://docs.docker.com/get-started/
But it does exactly what you want, running app in background in a container.
You can close everything your app will still run.
Enjoy reading !

I will try the docker style next time because im running out of time.
I found a way using :
"screen" command !

Actually the process stop after few hours with screen

Related

How to restart NodeJS with cPanel

I need to know what to use from root side of cPanel based server to restart NodeJS app, for example, if process terminated now for some reasons NodeJS app will not start until I manually start it, same if server restart I need manually to restart it.
Also, this is case for several accounts on server, command should allow more apps to be restarted/started.
Any help would be great
Here is an automated way to do the it:
- Find if node server [eq. server.js] is running or not.
- If server is not running, restart by "nodemon server.js".
- Else if server is running, do nothing.
You can code this in bash script [sample code below] and set up a CRON job in your cpanel to run it after a particular time.
#!/bin/bash
NAME="server.js" # nodejs script's name here
RUN=`pgrep -f $NAME`
if [ "$RUN" == "" ]; then
nodemon server.js
else
echo "Script is running"
fi
I'd recommend running your node app using PM2.
npm install pm2 -g
pm2 start app.js
pm2 restart app
If you are using node or nodemon on Linux machine I'd recommend using PM2 to manage the service. It is a lot more stable that nodemon and offers other production level features like console.log to console.error file and
https://pm2.io/doc/en/runtime/features/commands-cheatsheet/
I just used a crone job to run the following command once every 5mins
~/bin/node ~/backends/api/app.js
I was having problems with nodemon, it was saying it's not a command blah blah so I thought of just directing straight to node and directly to my app.
This is working for my use case coz the app bails if the addr is being used. So if it crashed then it will restart it since it won't occupy the addr.

Do I need to manually restart my app on EC2 if the server restarts?

This is my first time with EC2 so keep that in mind. I spun an EC2 instance and put a really basic nodejs/express app up on it. I connected to the ec2 server via the terminal on my personal computer and ran node app.js to start the app and everything is running fine. The part I am confused about is how long this will run for. Ideally, I just want it to sit there and not touch it and have it run for hopefully years. Will it do this? If not what do I need to do? What if the server restarts for some reason? What is the common practice here?
Go to root directory of your project and type this command to run the server permanently.
sudo npm install forever -g
forever start -c "node app.js" ./
This blog may be helpful, in setting up node for production environments

I can't keep my server running once I close terminal or my ssh session

I have never set up a server before but since Parse announced that they are closing down I thought I might give it a shot. I have followed along with this tutorial and have managed to migrate my Parse database across to digital ocean.
When I call npm run start everything works fine. I can query for data and create new objects all from my iOS app. But there is just one problem. How do I keep the server up and running even when terminal is not running from my Mac.
When I call npm run start this is what gets logged in terminal:
> parse-server-example#1.0.0 start /var/www/parse
> node index.js
[TypeError: Cannot read property 'Kerberos' of undefined]
DATABASE_URI not specified, falling back to localhost.
parse-server-example running on port 1337.
I know that this is probably a noob question and yes my knowledge is quite limited, so if you could help me then that would be great!
Thanks for your time!
Okay so I have just found the answer after posting a question on the Digital Ocean question page, instead of running npm run start I should have been doing nohup npm start &
use screen to create a new session https://tournasdimitrios1.wordpress.com/2010/11/04/linux-the-screen-command-a-must-for-ssh/
start your server
detach session
return to running session when needed
+1 to Lev for his answer, I don't have enough reputation to upvote his answer.
Another option is tmux, like screen, you create a session then start your app and detach when done and your app will continue to run.
I see this document can help you. https://www.npmjs.com/package/forever. I tried in window. It is OK.
Those are my comment
*** Run background code with schedule
Linux: nohup file-nodejs.js &
Window:
Install forever: npm install forever -g
Start/Stop : forever start file-nodejs.js | forever stop file-nodejs.js (restartall | restart | stopall)
Reference : https://www.npmjs.com/package/forever

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.

How do I leave Node.js server on EC2 running forever?

As you can tell by my question, I'm new to this...
I built my first website, I set up my first Node.js server to serve it and then pushed everything live on EC2.
I tested everything on my EC2 IP address and everything seems to be working.
Now up until now, I've been testing my app locally so it makes sense that whenever I closed the terminal, app.js would stop running so nothing would be served on localhost.
Now that my server is on EC2, the same thing happens ("obviously" one could say..) whenever I close my terminal.
So my question is how do I keep my Node.js server running on EC2 for like... forever..so that my site stays live.. forever :)
I read something about a node module called "forever" but I'm wondering (being new and all..) why isn't this "forever" functionality a default setting of the Node.js-EC2 system ?
I mean, correct me if I'm wrong, but isn't the whole point of setting up a web server and pushing it live to have it stay live forever? Isn't that what servers are supposed to do anyway (infinitely listening for requests) ? And if that's the case why do we need extra modules/settings to achieve that ?
Thanks for your help.. As you can tell I'm not only looking for a solution but an explanation as well because I got really confused.. :-)
EDIT (a few details you might need) - After installing my app on EC2 these are the steps that I follow on the terminal (The app is running on Amazon Linux by the way) :
I type ssh -i xxxxxxxxxxx.pem ec2-user#ec2-xx-xx-xx-x.eu-west-1.compute.amazonaws.com on the
terminal
After logging onto the Amazon machine I then go to the relevant folder and execute node app.js
There are 3 folders in the machine : node, node_modules and *name of my app*
app.js resides in *name of my app*
After that, the site goes live on my EC2 IP
Once I close the terminal, everything is switched off
Before you invoke Node.js, run the command:
screen
This will create a persistent environment which will allow your process to keep running after you disconnect.
When you reconnect, you can use this command to reconnect to that environment:
screen -r
Here's a random link to learn more about screen:
http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
However, this won't help you if your EC2 instance restarts. There are many different ways to do that. Adding your startup command to /etc/rc.local is one way. Here's a link to an Amazon guide which includes adding something to /etc/rc.local.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html
I worked with the valid answer for a while but some times the screen just end with no reason also screen has no balance loader and others features that in a production enviroment you should care , Currently I use a npm component to do this job.
https://www.npmjs.com/package/pm2
This is so easy to use.
$ npm install pm2 -g
then just start your app with pm2 like this
$ pm2 start app.js
In the above link you can find diferents tasks to perform if you need.
Hope this help the newbies like me.
There's a better way. Use forever.js.
See it here: https://github.com/foreverjs/forever
This is a nice tutorial for how to use chkconfig with forever on CENTOS.
http://aronduby.com/starting-node-forever-scripts-at-boot-w-centos/
Or use tmux
Just Enter a tmux screen run node server
Ctrl+b Hit D and you're done.
I am very late to join the thread and seems its basic problem with every newbie. Follow the below to setup properly your first server.
follow the step on the ec2 instance(before doing this make sure you have a start script for pm2 in your package.json file):
npm install pm2 -g
pm2 startup systemd
See the output and at the last line it must be like..
You have to run this command as root. Execute the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup
systemd -u sammy --hp /home/sammy
Take the last line command and run again with root privilege.
(before running the next command, Provide a new start script for pm2 in your package.json file e.g: "pm2-start": "pm2 start ./bin/www")
npm run pm2-start
for more info follow the link.
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
If you are using a Ubuntu EC2, better to use the following we have been using this for the past 6 years and have had no issues with this.
sudo npm i -g forever
Now start your main, example
forever start index.js
forever start src/server.js
To stop the server use the following command
forever stop index.js
To list multiple servers running forever
forever listall

Resources