Skip to content
Advertisement

AWS – Ubuntu User Session Stops Spring Boot App

this is my first deploy on AWS. I have a Spring Boot API on an EC2 AWS instance. To run my API I run the command ‘java -jar app.jar’, this works for a while, but when the session for the ubuntu user is logged out my API stops. Then I have to run the command ‘java -jar app.jar’ and so over and over again…

Aug 17 21:17:01 ip-172-32-12-59 CRON[117596]: pam_unix(cron:session): session opened for user root by (uid=0)
Aug 17 21:17:01 ip-172-32-12-59 CRON[117597]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug 17 21:17:01 ip-172-32-12-59 CRON[117596]: pam_unix(cron:session): session closed for user root
Aug 17 21:21:39 ip-172-32-12-59 sshd[117226]: pam_unix(sshd:session): session closed for user ubuntu
Aug 17 21:21:39 ip-172-32-12-59 systemd-logind[452]: Session 26 logged out. Waiting for processes to exit.
Aug 17 21:21:39 ip-172-32-12-59 systemd[1]: session-26.scope: Succeeded.
Aug 17 21:21:39 ip-172-32-12-59 systemd-logind[452]: Removed session 26.

What can I do so that my API does not stop when the ubuntu user is logged out?

Advertisement

Answer

You should run the application as service.

You can reference this articles https://dzone.com/articles/run-your-java-application-as-a-service-on-ubuntu

sudo vi /etc/systemd/system/my-webapp.service
[Unit]
Description=My Webapp Java REST Service
[Service]
User=ubuntu

WorkingDirectory=/home/ubuntu
ExecStart=/home/ubuntu/my-webapp

SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

vi /home/ubuntu/my-webapp

#!/bin/sh
sudo /usr/bin/java -jar app.jar

You can find the java location with which java.

chmod +x /home/ubuntu/workspace/my-webapp
sudo systemctl daemon-reload
sudo systemctl enable my-webapp.service
sudo systemctl start my-webapp
sudo systemctl status my-webapp
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement