How to start a java service from Jenkins - java

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.

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 do I make my server application run on an EC2 instance?

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

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

Building a docker image doesnt stop because of minecraft server continuing to run

So I have been trying to learn docker for a few days now and set a first goal for me.
I want to run a spigot server inside a docker container and later on the road combine that with a BungeeCord network.
I have run into problem.
My dockerfile runs without problems but once it reaches the point where it starts the minecraft server, the images stops building.
I think this is due to the server continuing to run and not returning a code 0 to show docker to keep on running.
Am I wrong with my idea, and if not, how can I fix the problem?
Here is my Dockerfile:
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install openjdk-7-jre icedtea-7-plugin -y
RUN apt-get install wget -y
RUN mkdir mc_server && cd mc_server/
RUN wget http://getspigot.org/spigot18/spigot_server.jar
RUN java -Xms1536m -Xmx1536m -Dcom.mojang.eula.agree=true -jar spigot_server.jar nogui
This way the server starts up but docker never finishes building.
I hope I made my problem clear.
Greetings,
Joel
Replace that last RUN with CMD.
RUN / ADD / .. are used to build the static container environment where you want to run your application in. Everything that happens before running the actual application.
CMD and ENTRYPOINT define what's supposed to happen inside the container once you docker run it. This is where the startup script / call goes for the program.
The result of the Dockerfile is similar to a computer that's shut down but has everything installed on the harddrive including a script that autostarts the application. Turn it on and everything starts to run.
PS: https://hub.docker.com/search/?q=spigot&page=1&isAutomated=0&isOfficial=0&starCount=0&pullCount=0 there are several existing images

Jenkins job list changes when running as a service and from CLI

I was running jenkins as a service on my windows machine, and I had setup a few jobs. Now, when I shutdown the service and ran the same jenkins from command line, using "java -jar jenkins.war", all jobs are gone. Now, I thought it would delete configuration, and i started it back as service. Now, all the jobs were back again. Why does this happen?
I solved this by setting JENKINS_HOME to the jenkins folder, and the launching java -jar jenkins.war

Categories