How to keep Node.JS app running in the background?

How to keep Node.JS app running in the background?

Written by Avian on Sep 7th, 2021 Views Report Post

There are many ways to keep a Node.JS app running in the background after you closed the terminal or SSH connection. Here are some of the ways I've done just that.

PM2 (Multiplatform)

PM2 is by far my favorite way to manage my Node.JS apps. With features such as watch & reload, an API, and startup scripts it is an all in one deal.

Installation

Installation is easy, just run the command below, if you use yarn the installation is the same.

  • NPM:
npm install pm2 -g
  • Yarn:
yarn global add pm2

Usage

Once installed it's as easy as running the command below

pm2 start app.js # or your file name

And boom, your Node.JS app will be up and running. If you want more features such as restarting the app when it crashes you can add --watch to the end of the start command and it will watch for crashes and restart when it detects one.

pm2 start app.js --watch

To see all running apps run this command:

pm2 list

Termius_AOoKgGDpme.png

Take a look at the documentation to see all of the options you can pass to the CLI.

Extras

PM2 offers their own service that you can control/monitor your PM2 server from an online web panel. If you want a "GUI" view of your processes you can run the pm2 monit command and it will give you an interface that is nicer than a table.


Screen (Linux only)

Credits to Bobby Iliev

According to the documentation, screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. When you start a screen session you can run commands as if you were using a regular SSH connection.

Installation

  • Ubuntu/Debian:
sudo apt update -y
sudo apt install screen
  • CentOS:
yum update -y
sudo yum install screen

Usage

Once installed you can start a new screen session by running this:

screen -S SESSION_NAME

This creates a new screen session and allows you to run any commands, you can start your app normally like below:

node app.js

Disconnecting

Now you must be wondering, how do I get out of this session? Well its very easy, you just use a combination of keys. CTRL + A (this indicates that you are running a command on the session) then D (for disconnect)

CTRL + A -> D

Disconnect & Kill Process

If you want to kill the process when you exit you do the same process of CTRL + A then instead of D you put K (for kill). Once you input that it will ask you if you really want to kill the session, just hit Y and it will kill the process and bring you back to the original session.

CTRL + A -> K

Getting back in a session

If you want to get back into a session you can run this below:

screen -r PROCESS_ID_OR_SESSION_NAME

In case you forgot your session name or ID you can see all of the current running screens by running this:

screen -ls

It will give you a list of sessions, here is an example of mine:

  • If you didn't name your session it will look like this: 48927.pts-0.localhost
  • If you did name your session it will look like this: 48937.SESSION_NAME

The numbers before the first . is the session ID or process ID, if you named your session it will look like the second example.

These are just a few of the commands you can run with screen if you want to see them all visit the documentation.

Conclusion

These were 2 of the ways I keep my Node apps running in the background whenever I close my terminal, if you want to know more about these programs you should look into their documentations.

Thanks for reading!

Comments (0)