How to keep running java aplication on aws?

2.2k views Asked by At

I am new to Java. I have hapi fhir server running on AWS by cloning this repository (https://github.com/hapifhir/hapi-fhir-jpaserver-starter)

I run my server with follwing command: "sudo mvn -e jetty:run"

--

My Problem:

As soon as I log out of AWS, my server stops. When I am logged in to my AWS instance via the .pem file, AWS instance running with ubuntu 18.04 LTS with nginx server.

Thanks

2

There are 2 answers

1
Thanh Nguyen Van On

run it as daemon:

"sudo mvn -e jetty:run &"

The & makes the command run in the background.

From man bash:

If a command is terminated by the control operator &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0.

4
Ananthapadmanabhan On

The ideal approach to execute or setup a java application on AWS is to run it as a daemon by setting up systemd script or init in linux.

In your case the application stops as soon as you close the terminal, because you are starting it in the terminal without the nohup command, when the terminal is closed the application is also stopped since the controlling thread is stopped. If you just want to launch the application on a separate background thread without going through the hassle of actually setting it up as a service in linux , you can use the nohup command (setting up a systemd to register the java application as a service is the preferred approach) :

nohup java -jar yourjarName &