others-how to kill a tomcat process by dynamically grep its process ID (PID) in linux system ?

1. Purpose

In this post, I would demo how to dynamically grep apache tomcat’s process ID(i.e. PID) , and then kill the process by the PID, at last I would write a retart script to do this job automatically.

2. The System

  • Linux System

3. The solution

3.1 The old method

Suppose we have a apache tomcat web server running, normally , we can start and stop the tomcat process by these commands:

Suppose we are in the tomcat’s root directory (/opt/tomcat-bswen):

$ bin/shutdown.sh  # stop tomcat

$ bin/startup.sh  # start tomcat in background

But if we use these scripts to restart a tomcat, we would found some application errors that complaining about the un-released resources during the restart process. We should stop the tomcat process thoroughly and completely by killing the process.

But we know that if we want to kill a process, we should know its PID at first:

$ kill -9 <PID>

How to know the PID of the tomcat process?

3.2 Catch the PID of the running apache tomcat’s PID

Now we should get the PID of our running apache tomcat. We can do as follows:

Suppose our tomcat is under directory tomcat-bswen:

ps -ef | grep tomcat-bswen | grep java | awk ' { print $2 } '

After running the above command, we should get a number like this:

5048

3.3 Kill the process by its PID

Now we have the dynamic PID of the running process, then we can kill it like this format:

$ kill -9 $(expression)

where the express can be replaced with:

$ ps -ef | grep tomcat-bswen | grep java | awk ' { print $2 } '

So the whole command should look like this:

$ kill -9 $(ps -ef | grep tomcat-bswen | grep java | awk ' { print $2 } ')

Before executing the above command, for test purpose , we should test it as follows:

$ echo $(ps -ef | grep tomcat-bswen | grep java | awk ' { print $2 } ')
5048

It should display the number of the tomcat process, then we can verify the number belongs to the running tomcat process by the following command:

$ ps -ef|grep 5048

root      5048     1  1 10:55 pts/0    00:00:24 /java -Djava.util.logging.config.file=/opt/tomcat-bswen/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=1048 -Xms64M -Xmx268M -Dignore.endorsed.dirs= -classpath /opt/tomcat-bswen/bin/bootstrap.jar:/opt/tomcat-bswen/bin/tomcat-juli.jar -Dcatalina.base=/opt/tomcat-bswen -Dcatalina.home=/opt/tomcat-bswen -Djava.io.tmpdir=/opt/tomcat-bswen/temp org.apache.catalina.startup.Bootstrap start

Now everything is verified, we can confirm the kill as follows:

$ kill -9 $(ps -ef | grep tomcat-bswen | grep java | awk ' { print $2 } ')

3.4 The restart script

Now we want to write a restart script to stop tomcat and start tomcat automatically, just do as follows:

/opt/tomcat-bswen/restart.sh

cd /opt/tomcat-bswen
kill -9 $(ps -ef | grep tomcat-bswen | grep java | awk ' { print $2 } ')
sleep 3
/opt/tomcat-bswen/bin/startup.sh

Now we can restart apache tomcat easily as follows:

$ chmod +x /opt/tomcat-bswen/restart.sh
$ /opt/tomcat-bswen/restart.sh
Using CATALINA_BASE:   /opt/tomcat-bswen
Using CATALINA_HOME:   /opt/tomcat-bswen
Using CATALINA_TMPDIR: /opt/tomcat-bswen/temp
Using JRE_HOME:        /lib/jdk1.8.0
Using CLASSPATH:       /opt/tomcat-bswen/bin/bootstrap.jar:/opt/tomcat-bswen/bin/tomcat-juli.jar
Tomcat started.

It works!

4. About awk

4.1 What is awk command

Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing.

Awk reads information from the input file or standard input. Like sed, the information is read line by line. The difference is that the awk command treats a line in a text file as a record, and treats a certain part (column) in a line as a field of the record. In order to manipulate these different fields (columns), awk borrows a method similar to positional variables in the shell, using the order of $1, $2…$9 to represent different columns, and $0 to represent the entire row. Different fields can be separated from different fields in a specified way. The default separator of awk is a space. The awk command allows the use of “-F separator” to specify the separator.

For example,

awk '{print}' bswen.py

would print all lines of the file: bswen.py, if want to print by columns, just do as follows:

awk '{print $3}' bswen.py

The above command would print the third field of each line in the bswen.py.

4.2 What is awk ‘ { print $2 } ‘)

  • $2: indicates the second field

  • print $2: print the second field

  • awk’{print $2}’ : read the specified file line by line, use a space as a separator, and print the second field

    For example, there is such a file(bswen.txt):

    a1 b1 c1 d1
    a2 b2 c2 d2
    

    The result of the execution:

    awk '{print $2}' bswen.txt
    

    the output:

    b1
    b2
    

6. Summary

In this post, we demonstrated how to catch a process’s PID dynamically by using grep and awk command , and then we write a restart script to restart the tomcat process automatically, actually you can replace the tomcat with anyother running processes, for example, a python process or a java process. At last, we learned about the awk command, which is a useful tool in Linux system to catch lines or columns of a text file.