0

I developed a Java server (using Spring) and uploaded the final executable JAR to an EC2 instance using FileZilla. Now I want it to run.

I've connected via SSH and used java -jar server.jar to run my server, and it worked (I've tried accessing it). However once the SSH connection is closed the server obviously stops running as well.

How can I start my application in such a way so it keeps running?

Edit: Using the command screen explained here I was able to run it in background and so it keeps running.

1

3 Answers 3

1

The issue is not cloud dependent its the configuration you have to do to run your jar as a service in your system. If you are using Elastic Bean Stalk change systemctl to initctl in below example.

  1. Put the script commands you wish to run in /usr/bin/demoscript.sh
  2. Remember to make the script executable with chmod +x.
  3. Create the following file:

/usr/lib/systemd/system/demo.service

[Unit] Description=Demo Script [Service] Type=forking ExecStart=/usr/bin/demoscript.sh 
  1. Reload the systemd service files: systemctl daemon-reload
  2. Check that it is working with systemctl start demo
Sign up to request clarification or add additional context in comments.

Comments

0

You need to make it run as daemon process in linux.

There are many tutorial / templates available to create a daemon shell script. Quick google search shows github has many templates, so check them out.

Comments

0

You could try using systemd which is a Linux service manager. You can use it to run your service in the background.

To do that you need to first create a unit file that describes how systemd should manage your service (more info here).

sudo vim /etc/systemd/system/your-application.service 

Your file might look something like this

[Unit] Description=Java Application as a Service [Service] User=ec2-user #change this directory into your workspace #mkdir workspace WorkingDirectory=/home/ec2-user/workspace #path to the executable bash script which executes the jar file ExecStart=/bin/bash /home/ec2-user/workspace/your-script.sh SuccessExitStatus=143 TimeoutStopSec=10 Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target 

Then in your home directory /home/ec2-user/workspace you can create the bash script that will run your java application.

sudo nano your-script.sh 

Your script might look like this

#!/bin/sh java -jar your-application.jar 

All you need to do then is start the service with the command

sudo systemctl enable your-application.service sudo systemctl start your-application.service 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.