aspose file tools*
The moose likes Linux / UNIX and the fly likes delete a pid based on the netstat command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "delete a pid based on the netstat command" Watch "delete a pid based on the netstat command" New topic
Author

delete a pid based on the netstat command

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1378
How to delete a pid based on the netstat of grep particular port number ?
ps -ef | grep 1234| grep java | awk '{ print $8 }' | kill -9"

1234 is the port number
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

You need to extract the PID in a sub-shell and kill that. For example

kill $(ps -ef | grep 1234| grep java | awk '{ print $8 }')

where I'm assuming that you have made sure that

ps -ef | grep 1234| grep java | awk '{ print $8 }'

returns the correct PID. I'm not convinced it does !

Note - it is normally considered poor to use "kill -9" rather than just "kill".
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1378
I want to kill this process using java's Runtime.getRuntime().exec()

Runtime.getRuntime().exec("ps -ef | grep 1234| grep java | awk '{ print $8 }' | kill");


Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1067
    
  10

kri shan wrote:I want to kill this process using java's Runtime.getRuntime().exec()

Runtime.getRuntime().exec("ps -ef | grep 1234| grep java | awk '{ print $8 }' | kill");




As I have already explained, that is not the syntax required with 'kill' ! You use a sub-shell and not a pipe.

Note - invoking Runtime.exec() to do this you will also need to execute the command using a shell since the sub-shell command needs to be interpreted by a shell. I hope you have read ALL the sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and implemented ALL the recommendations.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
1. I'm not sure your selection is specific enough to not kill the wrong thing, but that is your concern.

2. print $8 doesn't give me the process id, I get process id from $2. Use whatever gives you the value you need.

3. There is more than one way to do this, I use xargs. I used kill -3 (thread dump) instead of 9 since I didn't actually want to kill my process.





kri shan wrote:How to delete a pid based on the netstat of grep particular port number ?
ps -ef | grep 1234| grep java | awk '{ print $8 }' | kill -9"

1234 is the port number
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: delete a pid based on the netstat command