Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute Engine and NodeJS, keep online

I have a website already developed with node.js and sails.js on "Compute Engine" but to start it, necessarily I have to write "node app.js" in the SSH console of browser. But there is a problem (since I am new to Linux and Google Cloud :)), if I want my Node app it keep online, the SSH window must be open, if I close, the Node application will terminate, like Ctrl+c. So it makes no sense to have my computer turned on, just to have the active window. So, how to keep my NodeJS app online without being in the presence of SSH console?. I know the question is illogic to some, but I would appreciate if you help me. Thanks

like image 865
Walter Chapilliquen - wZVanG Avatar asked Dec 29 '25 13:12

Walter Chapilliquen - wZVanG


1 Answers

First of all it is not related to Compute Engine nor Node.js.

You mention that the application will terminate if you press ctrl+c and that's correct because you are running your application in the foreground instead of background. To run your application in the background you can either launch your app like this:

node app.js &

Or you can launch with node app.js and then press ctrl+z.

But just sending the application to the background wouldn't help. As soon as you disconnect from your ssh session, all programs started (background or foreground) will receive a HUP signal, in node.js you can handle that signal but the default behaviour is to close:

On non-Windows platforms, the default behaviour of SIGHUP is to terminate node

One thing that you can do to ignore the SIGHUP signal is running your app as follows:

nohup node app.js &

If you do this, your application will continue to run even when you close your ssh session. Another way to achieve this is by using the screen.

This will help you on very early stage and for development but I don't recommend using neither of these things for production. Eg: if your application crash it should be restarted. The recommended way is to use a service manager that comes with the OS: upstart, systemd, init, to name a few.

like image 71
José F. Romaniello Avatar answered Jan 01 '26 06:01

José F. Romaniello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!