Question

keep a node js file running

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

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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