keep a node js file running

March 29, 2019 576 views
Node.js Ubuntu 16.04

Hi all , i have writting my little node js file that run mysql querys for me what i need to know is how i keep this running when i am not connected as it will need to do this when i am not at the computer

1 Answer

Greetings!

There are several ways to go about this. The dirtiest way (in my opinion) is simple but not fault tolerant, and it uses a screen session. So, for example, if this were Debian/Ubuntu:

apt-get update
apt-get install screen -y
screen -S nodeapp
node app.js

Then CTRL+A followed by CTRL+D to detach from the screen session. This persists when you log out of your SSH session.

However, I recommend something a bit more powerful if you're up for it. You can use PM2 as a manager for this, and we have a tutorial here:

https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps

Obviously you don't need all of that tutorial, but you should be able to extract what is relevant to you.

Jarland

by Jim Cassidy
Use these instructions to setup an Ubuntu server to run Node.js applications, including apps based on Express, Geddy, or Sails. This will help you avoid some security mistakes and will provide some surprising benefits.
Have another answer? Share your knowledge.