How to run spring-boot as a service in linux - spring-boot

I am trying to run a spring-boot application as a service in linux box so that I can start and stop it as a jenkins job.
As per the suggestions in for this question Spring Boot application as a Service I created the soft link
$sudo link -s /opt/jenkins/workspace/myapp/myapp.jar /etc/init.d/myapp
Now when I am trying to run it from the jenkins command prompt I get command not found
$ sudo /etc/init.d/myapp start
sudo: /etc/init.d/myapp: command not found
I am using spring boot 1.3 and java8

Did you check the executable flag (chmod +x myapp.jar)?

Related

Jenkins - Local checkout - Enable using script console

I am encountering below error. I am able to set the property using System.setProperty("hudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT", "true")
However, the issue still persists. Any pointers?
ERROR: Checkout of Git remote '<path to project folder>' aborted
because it references a local directory, which may be insecure.
You can allow local checkouts anyway by setting the system property
'hudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT' to true.
I found the info I needed and propably helps you too in
https://issues.jenkins.io/browse/JENKINS-68571:
So, follow these steps:
$ sudo systemctl stop jenkins
$ sudo systemctl edit jenkins
[Service]
Environment="JAVA_OPTS=-Dhudson.model.DirectoryBrowserSupport.CSP= -Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true"
$ sudo systemctl restart jenkins
As per https://issues.jenkins.io/browse/JENKINS-68571:
it seems the System Property is read during initialization, thus changing it in Script Console does not change it.
In Script console use property on class directly:
hudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT = true
Note that neither the System Property nor the class property persist across restarts.
A persistent solution depends on how you installed / start Jenkins.
If you are running via java -jar ..., add the system property there (java -Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true -jar ...).
Or, if you installed it using your systems package manager and your system is using systemd:
$ sudo systemctl edit jenkins
[Service]
Environment="JAVA_OPTS=-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true"
$ sudo systemctl restart jenkins

RabbitMQ Docker Bash Script Issue - .sh Not Found

I am trying to create a RabbitMQ image that installs the rabbitmq_auth_backend_http plugin, and also executes a java application (.jar) via a bash script file. However, when I run the container I get this output from RabbitMQ:
/usr/local/bin/docker-deploy.sh: 2: /usr/local/bin/docker-deploy.sh:
: not found
Starting rabbit-sidecar application
Enabling plugins on node rabbit#039e6bd1bbfd:
bbitmq_auth_backend_http
The following plugins have been configured:
rabbitmq_auth_backend_http
rabbitmq_management
rabbitmq_management_agent
rabbitmq_web_dispatch
Applying plugin configuration to rabbit#039e6bd1bbfd...
The following plugins have been enabled:
rabbitmq_auth_backend_http
set 4 plugins.
Offline change; changes will take effect at broker restart.
/usr/local/bin/docker-deploy.sh: 4: /usr/local/bin/docker-deploy.sh: rabbitmq-server
: not found
It's also not finding rabbitmq-server which is needed to restart rabbit for the plugin to work.
To note, the java application appears to be working as it should and is just logging what it normally logs when its spun up as its own container.
Dockerfile
FROM rabbitmq:3.8.2-management
COPY ./rabbitmq.conf /etc/rabbitmq/rabbitmq.conf
RUN apt -y update && apt -y install openjdk-8-jdk
RUN mkdir /sidecar
ADD ./target/demo-0.0.1-SNAPSHOT.jar /sidecar/demo-0.0.1-SNAPSHOT.jar
ADD ./docker-deploy.sh /usr/local/bin/docker-deploy.sh
RUN chmod 755 /usr/local/bin/docker-deploy.sh #755
ENTRYPOINT ["sh", "/usr/local/bin/docker-deploy.sh"]
Script
#!/bin/bash
echo "Starting rabbit-sidecar application"
rabbitmq-plugins enable rabbitmq_auth_backend_http; rabbitmq-server
# starts the sidecar
java -jar /sidecar/demo-0.0.1-SNAPSHOT.jar
My original custom image that worked was this:
FROM rabbitmq:3.8.2-management
COPY rabbitmq.conf /etc/rabbitmq/rabbitmq.conf
CMD ["sh", "-c", "rabbitmq-plugins enable rabbitmq_auth_backend_http; rabbitmq-server"]
Which worked fine, but obviously I needed to do what I am doing because I want to be able to run another service within the same container as it requires it.
I did notice that there is a docker-entrypoint.sh in /usr/local/bin. Is this something to be considered in this context?

How to install Spring boot app on Ubuntu server?

I have Ubuntu server on Digital Ocean and I wrote Spring web app and now I want to put it in production.
I upload it via FTP to the server and I open my console via Putty and I use this command:
java -jar name.jar
Spring is started after that and when I open my web app everything is working fine, but when I close my Putty session my Spring web app does not work anymore. It seems like when I close my Putty session that also Spring web app is closed.
How to solve this?
While what KLHauser suggested will work, but if the vm is restarted in the cloud (which happens) your application will not automatically restart. Also stopping your application with kill -9 is error prone and dangerous, because you accidentally may kill the wrong process.
See running as Linux service section of Spring Boot documentation on how to do that.
If you’ve configured Spring Boot’s Maven or Gradle plugin to generate
a fully executable jar, and you’re not using a custom
embeddedLaunchScript, then your application can be used as an init.d
service. Simply symlink the jar to init.d to support the standard
start, stop, restart and status commands.
The script supports the following features:
Starts the services as the user that owns the jar file
Tracks
application’s PID using /var/run//.pid
Writes
console logs to /var/log/.log
Assuming that you have a Spring Boot application installed in
/var/myapp, to install a Spring Boot application as an init.d service
simply create a symlink:
$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp Once installed,
you can start and stop the service in the usual way. For example, on a
Debian based system:
$ service myapp start
Just use java -jar name.jar & and the application is started in new process thread.
by adding also > log.txt directly at the end you would also have a log.

Spring Boot application as a systemd service: log file

I know it is possible to create a jar from Spring Boot application which can be used as a systemd service. I used this manual to create systemd service from my application on Debian Jessie OS. Eveyrthing works fine, but I can't find a way how to write logs to separate file insted of /var/syslog. As documentation says:
Note that unlike when running as an init.d service, user that runs the
application, PID file and console log file behave differently under
systemd and must be configured using appropriate fields in ‘service’
script. Consult the service unit configuration man page for more
details.
it should be configured in *.service file, but I can't find any appropriate options. Has someone any experience in this question?
Run the service with a sh process
[Service]
ExecStart=/bin/sh -c "/var/myapp/myapp.jar >> /var/logs/myapp.log"
KillMode=control-group
See this discussion in influxdb github repo https://github.com/influxdata/influxdb/issues/4490

How to deploy SpringBoot Maven application with Jenkins ?

I have a Spring Boot application which runs on embedded Tomcat servlet container mvn spring-boot:run . And I don’t want to deploy the project as separate war to standalone Tomcat.
Whenever I push code to BitBucket/Github, a hook runs and triggers Jenkins job (runs on Amazon EC2) to deploy the application.
The Jenkins job has a post build action: mvn spring-boot:run, the problem is that the job hangs when post build action finished.
There should be another way to do this. Any help would be appreciated.
The problem is that Jenkins doesn't handle spawning child process from builds very well. Workaround suggested by #Steve in the comment (nohuping) didn't change the behaviour in my case, but a simple workaround was to schedule app's start by using the at unix command:
> echo "mvn spring-boot:run" | at now + 1 minutes
This way Jenkins successfully completes the job without timing out.
If you end up running your application from a .jar file via java -jar app.jar be aware that Boot breaks if the .jar file is overwritten, you'll need to make sure the application is stopped before copying the artifact. If you're using ApplicationPidListener you can verify that the application is running (and stop it if it is) by adding execution of this command:
> test -f application.pid && xargs kill < application.pid || echo 'App was not running, nothing to stop'
I find very useful to first copy the artifacts to a specified area on the server to keep track of the deployed artifacts and not to start the app from the jenkins job folder. Then create a server log file there and start to listening to it on the jenkins window until the server started.
To do that I developed a small shell script that you can find here
You will also find a small article explaining how to configure the project on jenkins.
Please let me know if worked for you. Thnaks
The nohup and the at now + 1 minutes didn't work for me.
Since Jenkins was killing the process spun in the background, I ensured the process to not be killed by setting a fake BUILD_ID to that Jenkins task. This is what the Jenkins Execute shell task looks like:
BUILD_ID=do_not_kill_me
java -jar -Dserver.port=8053 /root/Deployments/my_application.war &
exit
As discussed here.
I assume you have a Jenkins-user on the server and this user is the owner of the Jenkins-service:
log in on the server as root.
run sudo visudo
add "jenkins ALL=(ALL) NOPASSWD:ALL" at the end (jenkins=your Jenkins-user)
Sign In in Jenkins and choose your jobs and click to configure
Choose "Execute Shell" in the "Post build step"
Copy and paste this:
service=myapp
if ps ax | grep -v grep | grep -v $0 | grep $service > /dev/null
then
sudo service myapp stop
sudo unlink /etc/init.d/myapp
sudo chmod +x /path/to/your/myapp.jar
sudo ln -s /path/to/your/myapp.jar /etc/init.d/myapp
sudo service myapp start
else
sudo chmod +x /path/to/your/myapp.jar
sudo ln -s /path/to/your/myapp.jar /etc/init.d/myapp
sudo service myapp start
fi
Save and run your job, the service should start automatically.
This worked for me on jenkins on a linux machine
kill -9 $(lsof -t -i:8080) || echo "Process was not running."
mvn clean compile
echo "mvn spring-boot:run" | at now + 1 minutes
In case no process on 8080 it will print the message and will continue.
Make sure that at is installed on your linux machine. You can use
sudo apt-get install at
to install at

Resources