Use Jenkins to deploy WAR file and restart application server - java

I am trying to deploy an application WAR file using Jenkins ANT to build the WAR and restart the server.
Once the WAR is built, I have written a shell script to stop the application server, deploy the WAR file and then restart the application server.
#Shutdown Tomcat
ssh tomcat#<servername> "cd /home/tomcat/app/bin/;exec bash ./shutdown.sh"
#waiting period
ssh tomcat#<servername> "sleep 10"
ssh tomcat#<servername> "cd /home/tomcat/app/webapps/;rm -r *"
#Copy the WAR file to webapps
#Start the tomcat server
ssh tomcat#<servername> "cd /home/tomcat/app/bin/;exec bash ./startup.sh"
It does shutdown the server, clear some temporary files but fails to start the tomcat server. Any idea why this would be happening?
I have tried to see if there are other processes or ports still in use guessing maybe the shutdown was not clean. However there doesnt seem to be any issue like that. When I manually start the tomcat server for the application by going to the tomcat/bin directory, Tomcat starts without any issues.
Thanks in advance.

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!

process manager for JAVA Jar Files for hosting

I want to deploy my java spring backend to AWS .
I figured the way to deploy jar file on aws. But the problem is as soon as I terminate the terminal deployment stops. I created a jar file on build and hosted on aws EC2 linux Instance by copying jar file there and running below command .
java -jar jarfile
I want this deployment to be persistent. Is there any tool similar to pm2 for nodeJs in JAVA so that I can run this jar file permanently. ?
Thanks for help.
You should use nohup to deploy jar file. It will run even though you close terminal
E.g:- nohup java -jar javaservice.jar > nohup.log &

How to restart/start/stop tomcat as non-root when tomcat is running as service under root?

I have seen lots of tutorials and tips around running tomcat on centos/ubuntu/linux.
I am using centos image in aws to run tomcat.
I did NOT do sudo yum install tomcat7*. This would have created a tomcat service account under with tomcat run will run. The reason I did not do this is because I need to copy (scp) the war file into webapps directory and I cannot scp directly as tomcat user.
Instead, I created a tomcat user with password.
I downloaded the tar.gz, unzipped and moved tomcat into /usr/share/tomcat7.
Following the instructions by this blog, I edited /etc/rc.d/init.d/tomcat as follows.
!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79.x86_64
CATALINA_HOME=/usr/share/tomcat7
export JAVA_HOME CATALINA_HOME
exec $CATALINA_HOME/bin/catalina.sh $*
Then I executed the following commands
sudo chmod 755 /etc/rc.d/init.d/tomcat
sudo chkconfig --level 2345 tomcat on
I rebooted the machine and on startup tomcat is fired by root user.
I could also see the tomcat page showing corectly.
Now, I scp'ed my war into webapps directory (as tomcat user).
stopped the tomcat and restarted
sudo /etc/init.d/tomcat stop
sudo /etc/init.d/tomcat start
I get permission denied error when trying to write to catalina.out. This is because the catalina.out file is touched by root after centos reboot and now when I restarted tomcat as "tomcat" user, it does not have permission to write to it.
I changed the ownership on catalina.out (chown tomcat:tomcat catalina.out). After this change, I see tomcat starting and logging correctly.
Now the question is what is the right way to do it.
1) I will have jenkins jobs copying the war file to tomcat. so to restart the service, I need to sudo, which I cant from jenkins.
2) changing ownership of catalina.out is not right thing to do. if machine reboots, catalina.out is owned by tomcat and root cannot write to it. so there are problems of this catalina.out owned by either root or tomcat user.
Thanks for suggestions

Restart remote tomcat which is running in remote machine?

I have the tomcat up and running on remote machine.
Now i need to create a batch file to restart the tomcat. I am using windows.
Stop tomcat
start tomcat
How can i write a batch file to restart it?
Thanks!
There are scripts in the bin directory of tomcat that will start and stop it called starup.bat and shutdown.bat IIRC. You will have to ssh in to remotely run them.
Also, if you're running as a Windows Service, there are ways to remote manage those service, but my Windows is rusty. You'll need to google for that.
You'll need to write a batch file either to SSH in or to restart the service.

Connect to remote server and start/stop the Tomcat that's running on that particular server using Ant?

The purpose is to:
connect to a remote server maybe via host: ip , port: 8181
stop Tomcat that's running on that server
deploy a .war file
restart Tomcat
Underneath are few approaches I have taken so far to achieve this task:
Approaches taken so far:
I have looked at the following solutions but none of them worked for me:
http://www.linuxquestions.org/questions/linux-newbie-8/start-tomcat-server-remotely-824472/ --Not efficient
http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Start_an_Existing_Application
http://raibledesigns.com/wiki/Wiki.jsp?page=TomcatAntTasks
--only start/stop application not the actual tomcat itself
http://cargo.codehaus.org/Remote+Container
-- Which does't start/stop tomcat that's running on that server
If you have ssh access to the server, then you might like to consider the JSch library which you can use in combination with SSHExec Ant Task to start and stop your server:
<sshexec host="somehost"
username="dude"
password="yo"
command="/etc/init.d/tomcat restart"/>
For start/stop tomcat
In linux environment
use SSH to reach the terminal of the remote machine.
from the terminal You can start/stop the tomcat
to start startup.sh
to stop shutdown.sh
Under windows environment
Install OpenSSHD for Windows - this will allow remote "console" sessions.
If you looking for something very "unix-like" then you can install cygwin.
http://www.worldgoneweb.com/2011/installing-openssh-on-windows-7/
to start startup.bat
to stop shutdown.bat
For deployment
Simply go to tomcat manager link on the below page(on any environment)
http://your_server_ip:port_number/
user credential are specified in tomcat-users.xml inside conf di

Categories