I am writing a small script to kill process(es) listening on particular port number. Here I am particularly looking at Java servlet-containers like Tomcat and JBoss, which sometimes don't complete their shutdown process and it still shows up as running process with ps or netstat. This needs to be kill-ed and for that knowing pid of that process is necessary. The netstat by default doesn't give only pid(s), so one has to use sed/awk/tr like utility to extract pid info. Does anyone know any program/utility which gives pid(s) based on listening port numbers? Or is there any option in netstat that I am missing?
Thanks, CS.
Hi ,
You can use this:
kill -9 `netstat -antp|grep 8080|grep java|awk '{ print $7 }'|cut -d'/' -f 1`
But if had to do the same thing I would search in the running processes instead using netstat, anyway you can extract the pid in the same way.
Silviu
On Tue, Sep 28, 2010 at 7:14 PM, Carlos S neubyr@gmail.com wrote:
I am writing a small script to kill process(es) listening on particular port number. Here I am particularly looking at Java servlet-containers like Tomcat and JBoss, which sometimes don't complete their shutdown process and it still shows up as running process with ps or netstat. This needs to be kill-ed and for that knowing pid of that process is necessary. The netstat by default doesn't give only pid(s), so one has to use sed/awk/tr like utility to extract pid info. Does anyone know any program/utility which gives pid(s) based on listening port numbers? Or is there any option in netstat that I am missing?
Thanks, CS. _______________________________________________ CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Silviu Hutanu wrote:
Hi ,
You can use this:
kill -9 `netstat -antp|grep 8080|grep java|awk '{ print $7 }'|cut -d'/' -f 1`
But if had to do the same thing I would search in the running processes instead using netstat, anyway you can extract the pid in the same way.
Yeah, but don't forget you may have shared memory, etc, left laying around. Those need to be cleaned out as well.
mark
Thanks for the replies.
I don't want to run this script as sudo/root user and hence not looking at lsof/fuser.
I think netstat and awk looks good solution for now. Following is what I have used for now. [[ netstat -nlp | grep 8082 | awk -F "/" '{ print $1 }' | awk '{ print $7 }' ]]
-- CS.
On Tue, Sep 28, 2010 at 12:33 PM, m.roth@5-cent.us wrote:
Silviu Hutanu wrote:
Hi ,
You can use this:
kill -9 `netstat -antp|grep 8080|grep java|awk '{ print $7 }'|cut -d'/' -f 1`
But if had to do the same thing I would search in the running processes instead using netstat, anyway you can extract the pid in the same way.
Yeah, but don't forget you may have shared memory, etc, left laying around. Those need to be cleaned out as well.
mark
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
Carlos S wrote:
I am writing a small script to kill process(es) listening on particular port number. Here I am particularly looking at Java servlet-containers like Tomcat and JBoss, which sometimes don't complete their shutdown process and it still shows up as running process with ps or netstat. This needs to be kill-ed and for that knowing pid of that process is necessary. The netstat by default doesn't give only pid(s), so one has to use sed/awk/tr like utility to extract pid info. Does anyone know any program/utility which gives pid(s) based on listening port numbers? Or is there any option in netstat that I am missing?
Thanks, CS.
fuser will do what you want. If you were looking for something listening on port 80, for instance:
[root@server ~]# fuser -n tcp 80 80/tcp: 3420 3718 3719 3721 3722 3723 3725 3726 3727 [root@server ~]#
The banner ( "80/tcp:" ) is sent to STDERR and the actual PIDs to STDOUT, so you could do something like this:
for procpid in $( fuser -n tcp 80 2>/dev/null ) do kill ${procpid} done
fuser requires root access.
For more, "man fuser"