background:
Daily work sometimes need to manually restart tomcat project, every time cd to the project path ---> cd bin ---> ./ startup.sh, has been repetitive cd cd cd very annoying. One time my colleagues say, how not to put it into an automated script it? Every time doing things like this, more than a waste of time. so I decided to write a script hands free.
Before considering python we have to realize, later abandoned. Currently python server version is 2.6.6, with the most current version of python 3.x.
demand:
I want to use the script to restart tomcat project, by the way (parameter: the name of the project) parameter passing.
Script Name: restartApp.sh
Run the script: sh restartApp.sh tomcat-8107
Code:
! # / bin / bash the SET -x # ps_result = `PS -ef | grep Tomcat | grep $ 1` #echo" $ {ps_result} " echo -e" \ the n-\ the n-\ the n-" PIDS =` PS -ef | grep Tomcat | grep $. 1 | grep -v 'grep' | grep -v 'restartApp.sh' | awk 'Print $ {2}' ` COUNT = 0 pid_num = 0 for PID in $ {} PIDS do pid_num PID = $ {} echo "pid_num: pid_num $ {}" # COUNT expr $ = `1` {COUNT} + COUNT = $ (($ COUNT +. 1)) echo" COUNT: $ {COUNT} " DONE IF [[$ COUNT -gt. 1] ] the then echo "a project has two abnormal process," the else the kill -9 $ PIDS} { echo "Kill PIDS $ {} Success!" # restart echo "reboot in ......." sh /home/lifesea/$1/bin/startup.sh - fi echo -e "\n\n\n"
Knowledge points:
-
#! / Bin / bash refers to the use of this script / bin / bash to explain the implementation, #! Special character representation, behind the root path of the shell is to explain this script.
- grep -v is a reverse lookup of meaning, such as grep -v restartApp is to find the line does not contain restartApp field
- pass shell variables are as follows:
Screenshot text Source: https: //www.cnblogs.com/bclshuai/p/7409773.html -
awk '{print $ 2}' Print second field
Problems encountered:
1. use anti-apostrophe, for the first time used
2.
Beginning: no add grep -v 'restartApp.sh' lead to check out the process there are three (except grep), as shown below
更改 后: ps -ef | grip Tomcat | grep $ 1 | grep -v 'grip' | grep -v 'restartApp.sh' | awk '{print $ 2}'
Execute the following statements draw the following diagram results
$ 1 change: grep tomcat-8107
#ps_result=`ps -ef | grep tomcat| grep $1`
#echo "${ps_result}"
3. On the server debug script because the script vim restartApp.sh not familiar reasons, resulting in low efficiency.
vim commonly used shortcut keys:
: Set number display line numbers
: Set -x verbose logging output
h forward
j Down
k improve
l right
4. Thanks znn from yesterday afternoon until today, has been assisting me locate the problem.