I am working on a Gradle Java project. Which starts Tomcat for testing and stops it later. I am supposed to kill this Tomcat instance when the test fails. I tried using "ps aux| grep tomcat | grep -v grap | awk {print $2}" command to get the process id and kill the process. But on Production, there will be so many Tomcat processes running simultaneously by many users, I just want the tomcat process started by my build.gradle for test execution. So how can I accomplish the task? Please provide me some guidelines.
-
Can you please share the intent to execute the test on `production` environment? Usually, `unit tests`, `integration tests` should be executed in your ci environment and deploy the build artifact to your environment (`dev`, `qa`, `production`). – Rishikesh Darandale Jul 17 '17 at 09:33
-
Do you really need to start Tomcat for your tests? Tests might be easier implemented and more deterministic if done in process. – aschoerk Jul 17 '17 at 09:34
-
Andreas, I am working on existing project. The test execution here requires Tomcat. – Jul 18 '17 at 05:14
5 Answers
You need to find a unique string in the output of 'ps aux' which differentiates your test tomcat and others'.
I currently use the below script to run 'shutdown.sh' first and then kill the PID as most of the times, the application stops but the process does not stop.
PID=`ps -ef | grep $JAVA_HOME/bin/java | grep "$TOMCAT_LOC"/conf | grep -v grep | awk '{ print $2 }'`
if [ $PID ]; then
echo tomcat is running with PID:$PID.
# Stop or Kill running Tomcat
if [[ -f $TOMCAT_LOC/bin/shutdown.sh ]]; then
[[ ! -x $TOMCAT_LOC/bin/shutdown.sh ]] && chmod a+x $TOMCAT_LOC/bin/shutdown.sh
$TOMCAT_LOC/bin/shutdown.sh >>/dev/null
sleep 20
fi
kill -9 $PID
sleep 3
else
echo tomcat is not running
fi
You may also look at configuring a PID file by editing the 'catalina.sh' which you can read later to find out your PID.
# CATALINA_PID (Optional) Path of the file which should contains the pid
# of the catalina startup java process, when start (fork) is
# used

- 66
- 5
Java JRE has tool called jps
in $JAVA_HOME/bin
folder.
It's similar to unix ps
command but for java only.
You can use it to determined exac java process you need.
Using this tool is more recommended and actually it is more useful, when you have more than one java applications is running on your host...
for example I have running h2 database and many other apps, but wanna kill only h2, so I can use jps to get it PID
$ jps
17810 GradleDaemon
17798 GradleWrapperMain
17816 h2-1.4.197.jar
17817 GradleDaemon
17818 GradleDaemon
18011 Jps
16479
and then just kill needed process:
kill -9 17816
and all other java apps will continue work normally. I not sure about tomcat, but I think it can be done in similar way, something like that:
kill -9 $(jps | grep tomcat | awk '{print $1}')
Lastly, little bit offtopic, but a specially to your case: correct way would be using start/stop/restart scripts provided by tomcat

- 3,273
- 1
- 32
- 30
so if you want to kill tomcat from that user from which you have logged in then try following and let me know if this helps you.
ps -ef | grep -v grep | grep `whoami` | grep tomcat
So by ps -ef I am listing all the processes then grep -v grep will remove the grep command's process then grep whoami
will look for your currently logged in user then grep tomcat will look only for tomcat process, test it once and if All is Well then you could kill it.
By the way how about tomcat stop script? In case it is there you could use that also.

- 130,504
- 14
- 57
- 93
-
Tomcat stop script fails sometimes. So, that is why I am not going with that approach. – Jul 17 '17 at 09:04
-
fine, you could try that above command and let me know if that helps you. – RavinderSingh13 Jul 17 '17 at 09:05
-
The above solution will kill all the Tomcat processes running by the user. I just want the one started for test to be killed. So, if we can capture the pid while starting the tomcat process and then use this id to kill. That would be more acceptable. – Jul 17 '17 at 09:22
-
ok, now my next question to you would be, how you are starting "test" tomcat for testing? I mean there will be some way to differentiate between all tomcats right? So if names of tomcats are different then you could find out with them too. Also I doubt here if you are having a single tomcat and different vms too in it, correct me if I am wrong here. – RavinderSingh13 Jul 17 '17 at 09:25
-
The correct way to terminate a Tomcat instance is via its own shutdown
command. You should not be thinking of processes, or PIDs, or kills, at all.

- 305,947
- 44
- 307
- 483
You can use shell variable $!
. It represents the PID of the most recent background command.
yourCommand &
CMD_PID=$!
echo $CMD_PID

- 163
- 8
-
AFAIS process starts from java. Btw `yourCommand & echo $!` is even simpler – Sergei Voitovich Jul 17 '17 at 09:51
-
Tomcat's startup command is a script that starts a Java process. `$!` will only give you the PID of the evanescent command processor that ran the script. – user207421 Jan 20 '19 at 01:02