The work needs to be responsible for the packaging and release of the project, and it is time-consuming and laborious to release the commands by hand. Today, I took the time to learn shell scripts. Taking the examples on other people's blogs as reference, I wrote two shell scripts for rapid deployment of tomcat projects.
The link to the original blog post is as follows:
========================================================================
To use, put the two shell scripts in the bin directory of tomcat, chmod +x to give the two scripts executable permissions, and execute them directly with ./deploy.sh or ./restart.sh.
Specific implementation functions:
1.deploy.sh first delete the project war package in the webapps directory (tomcat will automatically delete the folder extracted from the war package).
2.shutdown shuts down tomcat, if it has not been shut down after 3s, use kill -9 to kill the process.
3. Deploy the war package that has been uploaded to the specified location to tomcat and restart tomcat through restart.sh.
The code is attached below:
deploy.sh
#! /bin/sh bin=$(cd `dirname $0`;pwd) pid = `ps aux | grep tomcat | grep -v grep | grep -v deploy | grep $ {bin} | awk '{printf $ 2}' ` war=/data/kmss/resource/war/jl_server.war if [ ! -f "${war}" ]; then echo 'Please deploy the jl_server.war package to the /data/kmss/resource/war/ directory'; be echo "Deploy ${war##*/}..." if [ -f "${bin}/../webapps/jl_server.war" ];then echo "========rm jl_server.war=========" rm -rf ${bin}/../webapps/jl_server.war be #Close the tomcat process of this project if [ -n "${pid}" ];then echo "==========shutdown.sh============" ${bin}/shutdown.sh sleep 3 pid = `ps aux | grep tomcat | grep -v grep | grep -v deploy | grep $ {bin} | awk '{printf $ 2}' ` if [ -n "${pid}" ]; then echo "==========kill tomcat============" kill -9 ${pid} be be sleep 5 if [ -d "${bin}/../webapps/jl_server" ]; then echo "==========rm jl_server===========" rm -rf ${bin}/../webapps/jl_server be #Deploy war package to tomcat mv ${war} ${bin}/../webapps echo "Restart tomcat..." exec ${bin}/restart.sh
If the war package is deleted when tomcat is started, tomcat will automatically delete the folder extracted from the war package, but if the tomcat process is closed during the deletion process, the folder may be deleted in the future, so after closing the process, confirm whether the folder is delete.
In the reference blog post, the blogger uses $1 to use parameters to locate the war package location when entering commands. Because only a single war package needs to be uploaded in this project, the war package location is fixed directly in the script. If necessary, please refer to the blogger's blog post in the link in the header of the article.
restart.sh
#!/bin/sh echo "===========restart jl tomcat============="; bin=$(cd `dirname $0`;pwd) pid = `ps aux | grep tomcat | grep -v grep | grep -v restart | grep $ {bin} | awk '{printf $ 2}' ` dat=`date '+%Y-%m-%d %H:%M:%S'` echo "now time: $dat" echo "$pid" if [ -n "$pid" ]; then { echo ============shutdown.sh ================ $bin/shutdown.sh sleep 3 pid = `ps aux | grep tomcat | grep -v grep | grep -v restart | grep $ {bin} | awk '{printf $ 2}' ` if [ -n "$pid" ];then echo ============kill tomcat================= kill -9 $pid be echo ============startup.sh ================= $bin/startup.sh } else echo ============startup.sh ================= ${bin}/startup.sh be # $1 represents the first parameter entered after the command if [ "$1" == "-v" ]; then tail -f ${bin}/../logs/catalina.out be
restart.sh starts using ./restart.sh to restart tomcat. If the tomcat process is originally closed, it will be started directly.
Add the -v parameter to the command to print the tomcat log. ./restart.sh -v
Use deploy.sh to deploy the tomcat project with one click: