Using ps -ef | grep tomcat
I found a tomcat server that is running. I tried kill -9 {id}
but it returns \"No such process.\" What am I doing wrong?
ps -ef
will list all your currently running processes
| grep tomcat
will pass the output to grep
and look for instances of tomcat. Since the grep
is a process itself, it is returned from your command. However, your output shows no processes of Tomcat running.
Tomcat is not running. Your search is showing you the grep process, which is searching for tomcat. Of course, by the time you see that output, grep is no longer running, so the pid is no longer valid.
To kill a process by name I use the following
ps aux | grep "search-term" | grep -v grep | tr -s " " | cut -d " " -f 2 | xargs kill -9
The tr -s " " | cut -d " " -f 2
is same as awk '{print $2}'
. tr
supressess the tab spaces into single space and cut
is provided with <SPACE>
as the delimiter and the second column is requested. The second column in the ps aux
output is the process id.
ps -ef | grep tomcat | awk '{print $2}' | xargs kill -9
https://gist.github.com/nrshrivatsan/1d2ea4fcdcb9d1857076
Part 1
ps -ef | grep tomcat => Get all processes with tomcat grep
Part 2
Once we have process details, we pipe it into the part 2 of the script
awk '{print $2}' | xargs kill -9 => Get the second column [Process id] and kill them with -9 option
Hope this helps.
kill -9 $(ps -ef | grep 8084 | awk 'NR==2{print $2}')
NR is for the number of records in the input file.
awk
can find or replaces text
In tomcat/bin/catalina.sh
add the following line after just after the comment section ends:
CATALINA_PID=someFile.txt
then, to kill a running instance of Tomcat, you can use:
kill -9 `cat someFile.txt`