springboot project building non-stop server on AWS EC2 - spring-boot

I made java springboot project for the first time. SO need some advice.
I used gradle, war.
I git-pushed my project on AWS EC2.
and run java -jar filename.war which succeeded data communication with frontend.
But when I leave the terminal with ctrl +c , the java server stops.
Which way is the best to build non-stop server like Node pm2?
In my EC2 virtual machine, there are several projects and I use NginX to route each projects to different port numbers.
Thanks in advance!

Linux
You can use nohup:
nohup java -jar -Djava.awt.headless=true your_server.war > /path/log.out &
Later you can use:
ps aux // to get the $PID of your process
kill -9 $PID // to kill the process
Windows
Please check this answer

Related

How to run Spring Boot Application as a Service?

I have deployed my web application on company server by executing jar files then the project is running fine.But when i close the jar and again try to run the project on browser then client is not able to access the application.I want solution on this that if i close the jar then also client can access the application.
you can use this simple command to run a jar file as background service...
javaw -jar test.jar
after run this command you could not detect any change in cmd...and can close your command prompt. after 1 or 2 minute enter your URL in browser .. you will see your web program is running...
before run this command must be stop previously running jar for avoid same port conflict
for more details
When you close the cmd prompt, your java application will also be killed. To keep the java application running after closing the terminal you have to use the below command.
nohup java -jar app_name.jar &
just replace the app_name with your application name and run the command.

IntelliJ IDEA not stopping Tomcat server

I am running my Tomcat on my IntelliJ IDE. Whenever I stop my server, via the IDE, it never stops the server. Instead, I have to go manually kill it via the following command in my terminal:
ps -ef | grep tomcat
kill -9 <id>
I am not sure what is causing this issue. Is it safe to kill it every time?
IntelliJ IDEA just calls the standard Tomcat shutdown script. If it can't stop the server, the issue is most likely with the application you have deployed. If the app creates threads and doesn't properly terminate them on the server shutdown, Tomcat will not be able to stop gracefully. You can use jstack to see which threads are running and preventing the server shutdown.

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.

Running a server with Ansible

I just started using Ansible and I am having trouble running a server.
I have a server which can be started using java -jar target/server-1.0-SNAPSHOT.jar. However, this will start the server and keep running forever displaying output, so Ansible never finishes.
This is what I tried that never finishes:
- name: Start server
command: chdir=~/server java -jar target/server-1.0-SNAPSHOT.jar
What is the proper way to do this?
Either create a service, as #udondan suggests, or use an asynchronous task to launch your server. http://docs.ansible.com/ansible/playbooks_async.html
As #Petro026 suggested, your choices are asynchronous task or creating a service.
I would strongly suggest against the asynchronous task approach. It's a very fragile solution:
What if the host is restarted?
What if you run your playbook twice?
What if your server app just dies?
Your best bet is to create a service for it, and probably the easiest approach for it would involve using a process control system like supervisord, which is supported by ansible.
From the supervisor docs:
Supervisor is a client/server system that allows its users to monitor
and control a number of processes on UNIX-like operating systems.
Put that in a PID and send the output to nohup.
Something like this:
nohup java -jar target/server-1.0-SNAPSHOT.jar &
In your playbook:
name: Start server
command: chdir=~/server nohup java -jar target/server-1.0-SNAPSHOT.jar &
If you want kill the process kill -9 #numerofpid.

Nohup didn't work in Jenkins shell

I want my JBoss server to run in background, for that I am using nohup ./startPID.sh > /dev/null 2>&1& command. But when I pass same command in Jenkins, it doesn't work as expected. The console output in Jenkins says "command ran successfully" but in backend the JBoss server is still down.
Any inputs?
Regards
Manish Mehra
Use "at now" instead of "nohup"
In your job jenkins (execute shell) put :
set +e #so "at now" will run even if java -jar fails
#Run java app in background
echo "java -jar $(ls | grep *.jar | head -n 1)" | at now + 1 min
You could look at the JBoss management plugin
which spins up JBoss for you
This plugin allows to manage a JBoss Application Server during build
procedure.
With the plugin we can start/stop JBoss AS. It's very useful if we
need to run some integration tests against of the server. There is
also operation allows verification if artifacts are deployable.
It looks to be quite an old plugin but has cuurent users

Resources