This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

delete a pid based on the netstat command

 
kri shan
Ranch Hand
Posts: 1453
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1453
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1166
17
Java Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic