Linux Learning Summary (65) Tomcat startup script

Source: Internet
Author: User

Script learning has not been the discipline, before all swallowed-style learning, now decided to comb the next idea, to see exactly where is the weakness. Before asking an architecture Normal University God, ask the shell script how to learn, others answered the sentence, the shell is very simple ah. Now think of it as a really simple shell for someone with a lot of programming experience, from my knowledge of C and C + + in school, to be able to perceive. People's thinking must be first from the whole to the process. For the development of a program, must first from the overall logical framework to the specific details of processing. Next I went from a script to find my knowledge of the short board.

Title Requirements:
In a production environment, Tomcat is often unable to shut down completely, meaning that the shutdown.sh script that comes with Tomcat does not completely shut down the Java process. So you need to use the shell script to kill the process and start again.
Write a shell script to achieve the above functions. Note: The command to completely kill a process is kill-9 PID

Topic Analysis:
1 The topic requires the script to complete two tasks, close the Java process, and successfully start the Tomcat service. So it should be divided into two parts: Close and start. For the current topic, we only need to write the stop and start two functions, and then call the function separately. If we're going to make it into a/etc/init.d/, the following startup script. Then just add the case structure to the script and you can execute the stop and start options. If a restart function is required, then a restart function is defined, and the Restsat function is to stop and start first. Add one more condition to the case.

2 details to be aware of in the process. Kill-9 kill process more dangerous may cause some data loss, try to use less.
3 After starting to check whether the startup success, can be determined by the existence of a Java process.

The overall framework of the script is:

#!/bin/bashtomcatdir=/usr/local/tomcatstop(){}start(){}stopstart

The specific code is

#!/bin/bashtomcatdir=/usr/local/tomcat//Specify the installation path for Tomcat stop () {echo-n "stopping tomcat." Killall Java i=0 while [$i-lt] do if pgrep-l java &> dev/null//pgrep-l show process name and PID with data representation non-    Empty, for real.    Then killall Java sleep 1 echo-n "."        Each attempt outputs a point, giving the status hint. i=$[$i +1] Else echo-e "\033[32m[0k]\033[0m"//output green with brackets in the OK Character break fi done//program come in first with killall try to kill the process    If the successful direct display of the green font of the [OK] exit, if the failure, then try 10 more times.    If pgrep-l java &>/dev/null then pgrep java |xargs kill-9 &>/dev/null//Xargs means kill each process listed  Sleep 1 pgrep java |xargs kill-9 &>/dev/null fi if pgrep java &>/dev/null then echo "Java Process cannot kill, please check "Exit fi//again check the process, if still exist, with kill-9 forced kill, try two times." Cannot kill, output prompt message.    }start () {CD $tomcatdir echo-n "starting tomcat." ./bin/startup.sh 2>/tmp/start_tomcat.log i=0 While [$i-lt 3] do if! Pgrep java &>/dev/null then./bin/startup.SH 2>/tmp/start_tomcat.log echo-n "."    i=$[$i +1] Else echo-e "\033[32m[0k]\033[0m" break FI-Done if! pgrep Java &>/dev/null Then Cat/tmp/start_tomcat.log echo "Tomcat does not start, please check. "fi}//program comes in first try to start once, if successful direct output OK, if the process has not been found, then try three times. or fail, give a hint to quit. if! Rpm-q psmisc &>/dev/nullthen echo "Install the Psmic package. "Yum Install-y psmiscfi//before calling the function, check if the package with the Killall command is installed and not installed with Yum." Stopstart

Encapsulated as a script that can be started on boot

vim/etc/init.d/tomcatd#!/bin/bash#chkconfig:2345 80 20########################################################## ################### #this scripts is about Tomcat start or stop####################################################### ##################### #function usage () {echo "$ {Start|stop|restart}"}if [$#-ne 1];then usage exit 1FITOMCATD    Ir=/usr/local/tomcatstart () {CD $tomcatdir echo-n "starting tomcat." ./bin/startup.sh 2>/tmp/start_tomcat.log i=0 While [$i-lt 3] do if!        Pgrep java &>/dev/null then./bin/startup.sh 2>/tmp/start_tomcat.log echo-n "."    i=$[$i +1] Else echo-e "\033[32m[0k]\033[0m" break FI-Done if! pgrep Java &>/dev/null Then Cat/tmp/start_tomcat.log echo "Tomcat does not start, please check.    "Fi}stop () {echo-n" stopping tomcat. "        Killall Java i=0 while [$i-lt] doing if pgrep-l Java &> dev/null then killall java SleeP 1 echo-n "." i=$[$i +1] Else echo-e "\033[32m[0k]\033[0m" break fi-Done if pgrep-l Java &>/dev/nul    L then pgrep Java |xargs kill-9 &>/dev/null sleep 1 pgrep java |xargs kill-9 &>/dev/null fi if pgrep java &>/dev/null then echo "Java process does not kill, please check" Exit Fi}restart () {stopstart}case "$" I Nstart) Start; stop) stop;; restart) REStat;; Esac

Linux Learning Summary (65) Tomcat startup script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.