Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep alive express process after close the terminal

I'm trying to keep live a process after close the terminal. Is a node.js project with express. Basically, for other process I kept alive processes with:

$ node server.js &

I with that, was possible finish the SSH connection and close the console. But with express, I started my process with:

$ npm start &

And always, after one request, the process died. Exist a way to keep the process alive? I'm using EC2, with a Ubuntu instance.

like image 915
Benjamin RD Avatar asked Oct 25 '14 23:10

Benjamin RD


People also ask

How do I run node js app forever when console is closed?

js application locally after closing the terminal or Application, to run the nodeJS application permanently. We use NPM modules such as forever or PM2 to ensure that a given script runs continuously. NPM is a Default Package manager for Node.

How do I shutdown my Express Server?

To shut down an Express server gracefully when its process is killed with Node. js, we can use the @moebius/http-graceful-shutdown package. const express = require("express"); const { GracefulShutdownManager } = require("@moebius/http-graceful-shutdown"); const app = express(); const server = app.

How do I close a NPM terminal?

To end the program, you should be using Ctrl + C . If you do that, it sends SIGINT , which allows the program to end gracefully, unbinding from any ports it is listening on.


3 Answers

Use sceen

on your terminal type :

screen

then

npm start

And detach the session by pressing “Ctrl+a” immediately followed by “d”

Here to attach the sesion again : Source

It works on CentOS7

like image 151
BorisD Avatar answered Oct 22 '22 06:10

BorisD


I found the answer https://unix.stackexchange.com/questions/89483/keeping-a-process-running-after-putty-or-terminal-has-been-closed

Basically, you can use nohup process. (Execute Commands After You Exit From a Shell Prompt).

And I used:

$ nohup npm start &

And now is working good.

like image 36
Benjamin RD Avatar answered Oct 22 '22 08:10

Benjamin RD


I know that this is an old question, but for new visitors who identify with it, I'm going to go out on a limb here and guess that you are wanting to run a web app that is up all of the time: one that can service visitors to your IP address, night and day. If that is not the case, then screen or nohup can work fine, but if it IS the case, it is best practice to use a daemon like pm2 or forever.

Daemons are arguably the best way to keep a service running all the time... like, how does your Ubuntu instance know when you want to talk to it via SSH? It's due to sshd, the SSH daemon. The daemon has code that is always listening, just like you would want a web-app to do.

Also, pm2 and forever have some other nice features, like booting up the daemon again if your computer happens to crash-and-reboot.

like image 29
JCollier Avatar answered Oct 22 '22 07:10

JCollier