The Method of Starting and Stopping Tomcat Service by Writing Shell Script in Linux Environment

Time:2019-7-14

Step 1: Enter the console as an administrator, and create a new shell script in the specified directory. I’m going to call it tomcat.sh.

Step 2: Write shell scripts

#!/bin/bash 
tomcat_home=/usr/tomcat/apache-tomcat-8.0.48 
SHUTDOWN=$tomcat_home/bin/shutdown.sh 
STARTTOMCAT=$tomcat_home/bin/startup.sh 
case $1 in 
start) 
Echo "Start $tomcat_home" 
$STARTTOMCAT 
;; 
stop) 
Echo "Close $tomcat_home" 
$SHUTDOWN 
pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'` 
kill -9 $pidlist 
#!/bin/bash 
tomcat_home=/usr/tomcat/apache-tomcat-8.0.48 
SHUTDOWN=$tomcat_home/bin/shutdown.sh 
STARTTOMCAT=$tomcat_home/bin/startup.sh 
case $1 in 
start) 
Echo "Start $tomcat_home" 
$STARTTOMCAT 
;; 
stop) 
Echo "Close $tomcat_home" 
$SHUTDOWN 
pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'` 
kill -9 $pidlist 
stop) 
Echo "Close $tomcat_home" 
$SHUTDOWN 
pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'` 
kill -9 $pidlist 
# Delete log files. If you don't delete them first, don't delete the next line. 
rm $tomcat_home/logs/* -rf 
# Delete the temporary directory of Tomcat 
rm $tomcat_home/work/* -rf 
;; 
restart) 
Echo "Close $tomcat_home" 
$SHUTDOWN 
pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'` 
kill -9 $pidlist 
# Delete log files. If you don't delete them first, don't delete the next line. 
rm $tomcat_home/logs/* -rf 
# Delete the temporary directory of Tomcat 
rm $tomcat_home/work/* -rf 
sleep 5 
Echo "Start $tomcat_home" 
$STARTTOMCAT 
# Look at the startup log 
#tail -f $tomcat_home/logs/catalina.out 
;; 
logs) 
cd /mnt/alidata/apache-tomcat-7.0.68/logs 
tail -f catalina.out 
;; 
esac

Start up service:

../tomcat.sh start 

Out of Service:

../tomcat.sh stop 

Be careful:

1. The path to Tomcat in scripting is your own path to Tomcat

2. Errors may occur when the script is first run

Then you have to set permissions for scripts

chmod 777 ./tomcat.sh  

Then run it.

summary

Above mentioned is the method of compiling shell script to start and stop Tomcat service in Linux environment introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to developpaer.

Recommended Today

JVM + GC parsing (premise knowledge concatenation)

Premise preparation JVM GC garbage collection JVM virtual machine monitoring, tuning and troubleshooting Tomcat and microservice optimization 1. Premise review 1.1. JVM memory structure 1.1.1、 JVM Architecture Overview The gray part in the figure isThread private, there is almost no garbage collectionOrange partThread sharing, the main place where garbage recycling occurs What is the class […]