• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

delete a pid based on the netstat command

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic