How to run node js app forever when console is closed

0 votes
I connect to my remote server via ssh. Then I start my node.js app with Forever. Everything works fine until I close my console window. How to run node.js app FOREVER on my remote server even when I close my connection via ssh?

I just want to start an app and shut down my copmputer. My app should be working in the background on my remote server.
Oct 14, 2020 in Node-js by kartik
• 37,510 points
2,125 views

1 answer to this question.

0 votes

Hello @kartik,

You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.

Install upstart:

sudo apt-get install upstart

Create a simple script for your application that will look something like:

#!upstart
description "my app"

start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

env NODE_ENV=production

exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1

Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:

sudo start myapp
sudo stop myapp
sudo restart myapp

Hope it helps!!

Thank you!!

answered Oct 14, 2020 by Niroj
• 82,880 points

Related Questions In Node-js

0 votes
1 answer

How does a node.js process know when to stop?

Hello, node keeps track of all outstanding work ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,880 points
537 views
0 votes
1 answer

How to read environment variables in Node.js?

Hello @kartik, Yes,you can read environment variables in Node.js ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
1,360 views
0 votes
1 answer

How to write files in Node.js?

Hello @kartik, Currently there are three ways to ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
682 views
0 votes
1 answer

How to get GET (query string) variables in Express.js on Node.js?

Hello @kartik, Since you've mentioned Express.js in your ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
2,971 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,689 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,880 points
2,898 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,880 points
818 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,880 points
756 views
0 votes
1 answer

How to run app.js in nodejs?

Hello @kartik, Assuming I have node and npm properly installed on the ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,880 points
2,494 views
0 votes
1 answer

How to “Ping” from a Node.js app?

Hello @kartik, You could use exec to call the system ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,880 points
4,268 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP