0

I am able to run my node server on an EC2 instance by SSH-ing into the server and running the command node index.js when inside of the src file. The server works fine, but the issue is that I have to persist the SSH connection to the EC2 instance, otherwise it terminates the server process. I'm sure I'm doing something wrong, but I couldn't find a clear cut and dry solution online without getting too much into the weeds.

Also for anyone wondering, I SSH-ed into my github and installed git on the EC2 instance in order to pull the code from the repository to the instance, instead of using CodeDeploy. The reason is that I was getting vague errors with CodeDeploy like "there was an issue deploying your revision", so I decided to go the manual route for uploading the code. So I could use some solutions to common "gotchas" in this area as well. Thanks.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • 1
    A good answer can be found here https://stackoverflow.com/questions/4018154/how-do-i-run-a-node-js-app-as-a-background-service – Tim Apr 29 '21 at 10:04

2 Answers2

0

One very lame thing to do is to install tmux and just run the node server inside a tmux session - that should be used only for testing and dev experiments, not much more.

Another option is to wrap your node service as an OS service (e.g. systemd) and let the OS manage the lifecycle of the application for you.

Yet another option is to bundle the service as a Docker container and let Docker (or docker-compose) to run and manage it.

Finally, I'd prefer to choose a managed AWS solution like AWS Beanstalk to actually deploy a long-running service. They will take care of automatically applying patches to your server(s) and will make it easy to deploy new versions.

Juraj Martinka
  • 495
  • 1
  • 3
  • 8
  • Thanks! I appreciate all the different options. What's the reason tmux should only be used for testing/development? Would you say the same with wrapping the node service as an OS service? – Cameron Honis Apr 29 '21 at 09:10
0

To start service in background and deattach it from your ssh session execute the command on this way:

nohup node index.js &

nohup will deattach if from the terminal/ssh & will put it in background

Romeo Ninov
  • 5,263
  • 4
  • 20
  • 26