Reputation: 436
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.
Upvotes: 0
Views: 411
Reputation:
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.
Upvotes: 0
Reputation: 534
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
Upvotes: 0
Reputation: 1554
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
Upvotes: 0