How do I make my server application run on an EC2 instance? - java

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.

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.
Put the script commands you wish to run in /usr/bin/demoscript.sh
Remember to make the script executable with chmod +x.
Create the following file:
/usr/lib/systemd/system/demo.service
[Unit]
Description=Demo Script
[Service]
Type=forking
ExecStart=/usr/bin/demoscript.sh
Reload the systemd service files: systemctl daemon-reload
Check that it is working with systemctl start demo

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.

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

Related

Springboot application is stopping after closing the putty in unix box

How to deploy the jar/war file in unix box and it should not stop after closing putty.
Steps tried:
Created jar/war file from Springboot project
Login to Unix box with root user
Created a Project folder in /opt path
Copied the jar/war file in Project folder
Executed below command to run the application in putty:
Command: java -jar myapplication.jar
Application is running fine but issue comes when i close my putty. Application shutdowns.
Any suggestions...
you need to create your custom service in your machine
EX: In Linux we use sudo systemctl start MySQL like that you have to create,
Please refer to this documentation which will help you to create your own.
EX: sudo systemctl start my_java_spring_service
I hope it helps!

How to start a java service from Jenkins

Jenkins newbie here, I'm using Jenkins to build a SpringBoot app with Maven. What I have done ok so far:
Check out the code
Build the app
Copy the app to app folder.
However i could not complete this step:
Start the app as a server (standalone, not using Tomcat).
I use this command
java -jar app.jar &
but as long as Jenkins finishs the job, the app also quits (I don't see the log shows that the app exits, but when I checked, it did not run)
Jenkins runs on same server with the app (Amazon linux).
Any help is much appreciate.
Try using nohup. Something like this:
killall -9 app.jar
nohup java -jar app.jar > app.log 2>&1 &
But I strongly advise you to create a Docker image with your application to deploy it.
Best regards.

Centos java custom service

I have a script.sh that set some environment variable and start a java server.
#!/bin/bash
export JAVA_HOME="/opt/java"
export ....
nohup $JAVA_HOME/bin/java "$MEMORY_JAVA_OPS" -classpath "$MY_CLASSPATH" $MAIN_CLASS &
I would like to transform this script (now is launched by /etc/rc.d/rc.local) in a service.
I tried many examples found online and over StackOverflow.
I created myservice.service file using many templates found online... No one work!
one example is:
[Unit]
Description=MyService Java Process Restart Upstart Script
After=auditd.service systemd-user-sessions.service time-sync.target
[Service]
User=root
TimeoutStartSec=0
Type=simple
KillMode=process
#export JAVA_HOME=/opt/java/jdk-9
#export PATH=$PATH:$JAVA_HOME/bin
WorkingDirectory=/tmp/myworkdir
ExecStart=/path/to/myscript.sh
[Install]
WantedBy=multi-user.target
With some configurations, the service starts but the status command says that it is dead (while it is actually running). With others it does not start. With none it stops with the command stop ....
I tried Type=Simple, forking, oneshot... always some problem.
I would simply that after boot or when user launch systemctl start myservice, service start, and if after some time crash will be started again. And if I will run systemclt stop myservice it stops and not need to kill the process.
Firstly it need to be said, that concept "service" greatly differs in Linux/Unix and Windows environment. From your question seems to me you are looking for Unix solution.
In unix you typically register some statup and stop script/command. The startup script just runs your java application via java -jar app.jar. This application does business logic & also opens listening on some SHUTDOWN port.
The stop script/command just invokes another (or the same with different cmd parameters) java application which does nothing else just sending STOP command to original application's SHUTDOWN port.
You can look in more detail for example on tomcat startup/stop scripts - they are doing exactly this.
For windows is better to use some wrappers like WinRun4J or whatever else. Of course you can have one multiplatform maven archetype for "universal multiplatform" service like we do.
EDITED:
If you are still unsure how to configure it on Linux, read https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux
ExecStart will be the startup java -jar app.jar and ExecStop will be the stopping command java -jar app-stopper.jar

How can I restart a Java application when VM (virtual machine) is restart/start?

I am using java 1.8 and spring-boot-starter-parent 1.5.6.RELEASE and swagger2 2.6.1. We run the application in the VM using the following command,
nohup java -jar myApplication.jar &
My application is getting stopped when the VM is stopped. So manually I need to start my application. I don't want to do that manually. I need to restart my application programmatically or any script will do this that is also fine for me.
How do I restart my application when VM is started??
Kindly provide your inputs.
In a Linux distribution which uses systemd (official docs here) (such as Debian, Ubuntu, or Fedora), creating a service is simple:
We'll need to create a service file which tells systemd how to start your application. Create a file in /etc/systemd/system named something like myApplication.service containing these lines:
[Unit]
Description="A description of what my application does"
[Service]
ExecStart=/path/to/java -jar /path/to/your/myApplication.jar
[Install]
WantedBy=multi-user.target
(Derived from an example in the docs.)
Then run systemctl enable myApplication.service as root to enable it, which will make it run on boot.
There's lots more you can learn; the docs for systemd are quite good. You might take a look at the blog story which introduced systemd as well, as that provides something of a "sales pitch" for what it can do.

Spring Boot init.d not Not running (process not found)

I was trying to follow the instructions from here, where trying to run the Spring Boot app as init.d service but could not successfully.
I created the fully executable jar (myapp.jar) as mentioned and also created the symlink to /etc/init.d/myapp When I run the java -jar myapp.jar I could see the application start up successfully.
But when I try to use
service myapp status it says Not running (process not found)
service myapp start it says Failed to start
the documentation says "Assuming that you have a Spring Boot application installed in /var/myapp" I don't understand this point quite well. I copied the executable jar (via Jenkins) to /var/myapp. so it contains only one file which is jar. does this create the problem?
Any suggestions are appreciated.
Environment:
springBootVersion = '1.3.2.RELEASE'
JDK6 (yes it is)
Ubuntu 12.04
Sometimes, you may need to run: sudo systemctl daemon-reload for your new service to be loaded.
You must register your jar as service. Look at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#deployment-initd-service
In ubuntu you need reload deamon with sudo systemctl daemon-reload

Categories