Keeping ExpressJS Application Running After Terminal is Closed - node.js

My application, working on server, but then I close the terminal window and your application stops. How can I solve this issue.
How to make a application run permanently in background ?
Is there any Express plugins for solving this issues

You could found answer from ExpressJs official guide about Process managers for Express apps.
PM2 and Forever is recommended by official document for keeping your application running background and automatically restarts.

You can use shell screen:screen commands
Install screen : sudo apt install screen
Create a screen : Ctrl-a c
Go to your app folder and run app: node server.js (check any nodes api tp confirm your app is working)
Detach screen : Ctrl-a d
List screens to check your app is running: screen -ls
Close terminal.
PS: To attach the screen your app is running
List available screens : screen -ls
Attach screen : screen -r {pid}
Kill a screen using : screen -X -S {pid} quit

Related

AWS EC2: keep running node.js after exit console

I use aws ec2 to host web server with node.js and apache.
So far, I always need to login ec2 through terminal to run npm start.
I wanna make it keep running even if Im not on terminal. How am I supposed to setup for it?
I turned on https, but nothing happened.
I appreciate it in advance.
One option is using the screen command:
screen
You will reach a new bash prompt for that screen. Run your app.
To detach the screen, press Ctrl+A, then D to detach from that screen.
To reattach screen when you next SSH in, use
screen -ls
to see detached screens and use
screen -r xxxxx
with the screen number to reattach that screen.

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.

Tmux - How can I run node app forever after detaching tmux.?

I am making my first app in node js and i uploaded it on server. I have a terminal where I am running a session containing two panes, one for running mongod and second for running node app.js . Now both of them run as long as I keep it running. But how can we make sure after quitting the window it keeps running both of them mongod and node. I am using tmux, mongodb, express and node.
I tried tmux detach and tmux attach. They work. But for tmux detach first i have to quit the current command by ctrl + c and then i would be able to run tmux detach. Am I doing something wrong? Please Help
I presume this is for a development environment and not production. In production, keeping something running "forever" makes using a process supervisor to make sure it is restarted when it crashes.
In development, you can use <Prefix>-d to detach without cancelling the current command. The Prefix for Tmux is Control-B by default.

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

deploying nodejs and mongoose app in aws

I'm new to aws and recently I have been able to install node, mongod and also, FTPed project file to the server.
For mongodb,
I'm doing mongo in a separate terminal tab and starting service in another tab. I want to know how can I keep the mongo service running.
For node app,
Right now i'm doing node app in the server. How can I keep it alive too ?
Now the problem, is I open the browser with publicip:portno but nothing happens. How can I locate app and run it in the browser.
my app structure is
/
node
mongo
server.js and app related files
Using the Linux/Unix nohup command allows you to start commands that ignore the signals associated with the controlling terminal process terminating (SIGHUP). Adding the & to the command allows that command to run in the background and sending the output to /dev/null will ensure that your disk does not fill up with unnecessary log output. Here are some commands that should work:
nohup mongod >/dev/null &
node server.js >/dev/null &
This is more of a Linux/Unix command line issue I think. You can use the node module called forever to run your Node.js process in the background easily.
npm install -g forever
forever start YourScript.js
You can place an & at the end of the mongod command to place it in the background.
Ensure that in your node app you have the command app.listen("port number");
This is the "port number" that you should be using in your browser to render the page with the elastic IP from your AWS instance. Make sure that your elastic IP is configured to accept inbound requests.
To keep the service/app running in the background you can run the screen command then launch ur app/service (e.g. mongod OR node app.js). In the same terminal that your app is running press control + a + d, you should see
(detached)
printed on your screen.
This should keep your app/service running in the background.

Resources