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
Related
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!
I am trying to deploy my springboot standalone application in production in linux server.I am trying to use the out of the box tomcat server.
'java -jar jarname.jar'
This works fine , but if I close my putty session the application is unistalled.
Now I used
'nohup java -jar jarname.jar &'
this works good and my application is not shutdown even if my putty session is closed.But the logs will not roll correctly(I have configured log4j to create a new log file for each day) in this case. So was wondering if this is the right way to do this.
I have searched several documentation but was not able to find a correct solution for this problem.
Please help.
Thanks
Well I think it's better to use Linux services for running application, you can read here for example
And if you want to collect logs - better write them to the file.
Spring provides build-in me mechanics to do that
logging:
level:
root: INFO
file:
clean-history-on-start: false
max-history: 7
max-size: 10MB
name: some-name
path: /path/log/dir
total-size-cap: 0B
Why not package it into a Docker image and run that on the server?
Here are a few ideas:
https://medium.com/swlh/deploying-spring-boot-applications-15e14db25ff0
You can run your spring boot application as jar, but you need to create a service so you can execute your spring boot as daemon.
https://dzone.com/articles/run-your-java-application-as-a-service-on-ubuntu
With this, you can start or stop your application like
$ sudo service myspringbootapp stop
$ sudo service myspringbootapp start
I have a grails 2.4.3 application run on centos 7. My problem is that I can't start the application via terminal, because when I close terminal, the session is terminated.
My command to start program is:
export PATH=$PATH:/opt/setup/grails-2.4.4/bin
export JAVA_HOME=/usr/java/jdk1.7.0_80/
grails prod run-app
I have already try many ways:
i.e: nohup grails prod run-app (nothing change after running command),
setsid prod run-app (program terminated by accidentally after a time running).
Both the ways are not worked.
Please help me find a way to start Grails application in production precisely.
Thanks.
Do not use grails run-app in production. From the Grails 2.4.4 documentation
NEVER deploy Grails using the run-app command as this command sets Grails up for auto-reloading at runtime which has a severe performance and scalability implications
In Grails 2.x (for production), you need to deploy your application to a supported Java EE Container (Tomcat, Jetty, etc), which are listed on the same page in the documentation.
In Grails 3.x you can package your web app as a jar and run it like any other jar because it is built on top of Spring Boot and the container was packaged into the jar.
On linux, you can use the nohup (no hang-up) command to make sure that the command will not be killed when you close the terminal.
Create a sh (startup.sh) file containing your startup statements:
echo "export PATH=$PATH:/opt/setup/grails-2.4.4/bin
export JAVA_HOME=/usr/java/jdk1.7.0_80/
grails prod run-app" > startup.sh
make sure the script is executable
chmod +x startup.sh
then
nohup ./startup.sh
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
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.