Compute Engine and NodeJS, keep online - node.js

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

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.

You can install forever module (https://www.npmjs.com/package/forever) to your Google compute engine using SSH window.
npm install forever -g
Then, in your node server directory, execute your server using forever.
forever start [YOUR_SERVER_FILE.js]
Now, if you close the SSH window, your node server is still on !!
Good luck!

The best solution would be using a module called forever:
https://www.npmjs.com/package/forever
You can use it this way:
forever start your_script.js

Related

What is the correct way to start and stop simple Node.js scripts

I'm trying to create a simple Twitter bot to learn some Node.js skills.
It works fine on my local computer. I start the script with node bot.js and then close it with Ctrl + C.
I've uploaded the files to a server (Krystal hosting). I've ssh'd into the server and then used $ source /home/[username]/nodevenv/twitterbot/10/bin/activate. Which I think puts me into a Node environment (I'm not really clear what is happening here).
From here I can run node bot.js. My Twitter bot runs fine and I can leave the terminal. What I've realised now is that I don't know how to stop this script.
Can someone explain how I should be doing this? Is there a command I can enter to stop the original bot.js process? Since looking into this it looks like perhaps I should have used something like pm2 process manager. Is this correct?
Any pointers would be much appreciated.
Thanks,
B
You can kill it externally by nuking the process from an OS command line or in an OS GUI (exact procedure varies by OS). Ctrl-C from the shell is one version of this, but it can be done without the command shell that it was started in too by nuking the process directly.
Or, you can add a control port (a simple little http server running on a port only accessible locally) that accepts commands that let you do all sorts of things with the server such as extract statistics, shut it down, change the configuration, tell it to clear caches so content updates take effect immediately, etc... Shutting down the server this way allows for a more orderly shut-down from code within the server. You can even stop accepting incoming connections, but wait for existing http connections to complete before shutting down, close databases normally, etc...
Or, you can use a monitoring program such as PM2 or forever that in addition to restarting the server automatically if it should ever crash, they also offer commands for shutting it down too (which will just send it certain signals kind of like Ctrl-C does).

Node Backend stay running in GPC Compute Engine

I'm newbie using GCP and his Compute Engine Service. I've deploy a Linux Ubuntu Image an it's running like a charm, but I have a issue running Node JS backend.
Let me explain it better:
I'm connecting using Web Browser SSH terminal or GCloud Shell ssh, and it way works running node app.js my backend starts working. But after a time, the sessions stop and my backend service stop working as well. At this time every time when I need to work have to re-activate the service each time.
How could I do for this service works in background and not depends that my ssh terminal are opened?
Thanks a lot in advance.
What actually happen is you are starting your nodejs application using an client which is parent process. So if after sometime the connection is lost of some xyz seconds the parent process dies killing your node application. Now what you can do is use screen. On ubuntu you would do something like this.
sudo apt-get install screen
after successful install run the screen command. Now you will be thrown a brand new terminal. Here you can run your nodejs code which will never die. Since screen runs your application in background. More information here
A good solution could be to use a startup script. To insert a startup script into your already created instance you need to go to this link [1]. When you have your startup script inserted in the metadata field you just need to restart your Instance and then should work perfectly without depending of the ssh session.
[1] https://cloud.google.com/compute/docs/startupscript#startupscriptrunninginstances
I've created this npm package, to make your node app run as a service on your linux machine. Please try it out.
It creates a systemctl service on your machine and runs it as a background service.

Keep node in running state even after user log-off

How to keep a node application running in windows even when user logs off?
Also how to keep running a node http-server even after user log-off?
You have 2 great options. One is as mentioned in comments above Forever.
The other is PM2 which is easy to install and offers an incredible amount of options. I use this in all projects, but I cannot attest to the Windows version as I am on Linux & Ubuntu servers and work on a Mac. You can daemonize your node process, follow logs, cluster it and make sure the process reboots even with a server shutdown (it is a service).
windows task scheduler: execute node.exe: start in project folder: and argument (app.js)

Run node app forever with standard i/o?

I'm really new to node.js. My friend helped me set up a node app to run a java process I need running on a server at all times. It works perfectly, except the only way I can see the standard i/o is if I use node app.js. I've looked into both forever and pm2, however neither of these use standard i/o, which I really need for this server to run commands. Could somebody help me out please? Thanks!
Assuming you have a *nix-based server:
You could use GNU Screen.
Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.
In plain words, you would have access to always-running processes on server and their input-output from your local command line.
After logging in to your server, all you need to do is this:
Start new screen screen -S <name>
Run you java process
Detach from screen screen -d <name>
That's it! Your java process keeps running and you could interact with it by reattaching to the screen session like this: screen -r <name>
Useful Link: GNU Screen Quick Reference
Even cooler would be creating your own service using an Upstart script, which you could then call directly from your local machine with:
Create your own service using Upstart script.

How to set up a node.js development environment/server (Ubuntu 11.04)

I am trying to set up a development environment for node.js. I assumed at first that it requires something similar to the traditional, "localhost" server approach. But I found myself at a loss. I managed to start a node.js hello world app from the terminal. Which doesn't looked like a big deal - having to start an app from the console isn't that hard. But, after some tweaking, I found out that the changes aren't shown in the browser immediately - you need to "node [appName here]" it again to run.
So, my question is:
Is there a software or a tutorial on how to create a more "traditional" development server on your local machine? Along with port listening setup, various configurations, root directories etc (things that are regular in stacks like XAMMP, BitNami or even the prepackaged Ubuntu LAMP). Since I'm new at node.js, I can't really be sure I'm even searching for the right things on google.
Thanks.
Take a look at :
https://github.com/remy/nodemon
it'll allow you to do - nodemon app.js
and the server will restart automatically in case of failure.
To do this I built a relatively small tool in NodeJS that allows me to start/stop/restart a NodeJS child process (which contains the actual server) and see/change configuration option and builds/versions of the application, with admin options available on a different tcp port. It also monitors said child process to automatically respawn it if there was a error (and after x failed attempts stops trying and contacts me).
While I'm prohibited from sharing source code, this requires the (built-in) child_process module, which has a spawn method that returns a child process I guess, which contains a pid (process id) which you can use with the kill method to kill said child process. Instead of killing it you could also work with SIGINT an catch it within your child application to first clean up some stuff and then exit. It's relatively easy to do.
Some nice reading material regarding this.
http://nodejs.org/docs/v0.5.5/api/child_processes.html

Resources