Search process ids

Hi,

I have four tomcat instances named PNK, PNK1, PNK2, PNK3.
All are running on the same server.
To kill tomcat instance I usually do below for PNK1,PNK2,PNK3

kill -9 `ps -ef|grep tomcat|grep PNK1|grep -v grep|awk '{print $2}'`

But the above command does not work for PNK. Can some one help in modifying the above command that works for PNK instance?

Thanks.

Why aren't you running the shutdown script which gracefully stops tomcat instead of abruptly killing it, especially with the SIGKILL signal which should be used on a last resort, not routinely?

Anyway, this simpler command should do the job, assuming your system provides pgrep:

pkill -f "tomcat.*\<PNK\>"

or else

ps -ef | awk '/tomcat.*PNK/ {print "kill -9 " $2}' | sh

@vgersh This won't discriminate the PNK instance which is what the OP is looking for.

Hmmmmm.... and how does the 'pkill' kill a particular instance?

something along these lines then....

ps -ef | awk -v inst=1 '$0~ ("tomcat.*PNK" inst "$") {print "kill -9 " $2}' | sh
kill `ps -ef | grep '[t]omcat' | grep -w PNK1 | awk '{print $2}'`

The [t] trick avoids that grep finds itself.
The -w finds words, like \< \> boundaries.

1 Like

@vgersh The pkill based suggestion is using a regexp to kill the requested unnumbered instance.

Thanks Guys.