aspose file tools*
The moose likes Linux / UNIX and the fly likes how to kill a process(I know the port in which it runs) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "how to kill a process(I know the port in which it runs)" Watch "how to kill a process(I know the port in which it runs)" New topic
Author

how to kill a process(I know the port in which it runs)

ram mohan
Ranch Hand

Joined: Oct 17, 2000
Posts: 68
Hi,
I am a newbee for linux.I need advice in Linux.
I am running a web process which occupies a port.If I run it in the command mode I can simply kill that whenever I want by typing control+c.
But if I run it as a background( & ) process how to kill that particular process?I know the port in which it runs.
Presently what I am doing is by typing ps -ax|grep java and kill all java process.by that all java processed get killed.but I want to kill only that process.
I hope my question is clear enough.Pls help me
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Correctly me if I'm wrong, but I remember unix based run on process. Parents fork children, not threads. So if you killed the parent, children process will probably die.
You might want to try this:
ps -ax | grep process (httpd, ftpd, etc)
find out it's pid, then #kill pid
ram mohan
Ranch Hand

Joined: Oct 17, 2000
Posts: 68
Thanks Adrin.
As u said if the parent process get killed then the children process too will die.
But my problem is slightly different. There are couple of process with the same process name.Those are independent process.I kow the port in which they run.If I want to kill one of them which is running in a known port.
Basically what I want is to kill a process which is running in a known port say like 9050
I am sure now my question is clear.
Looking forward help
Ram

[This message has been edited by ram mohan (edited April 19, 2001).]
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Ram:
You really can't kill a process based on port, you can disable the port service in the /etc/service file or inetd.conf(name varies base don systems) file. Both these files basically control the system.
Also, I think you misunderstood my comment, even if the processes have the same name, they will have different process id (pid for short). run #ps -aux, then lokk for a column name pid. Identify the pid, then #kill pid. This should work.
ram mohan
Ranch Hand

Joined: Oct 17, 2000
Posts: 68
Ok Adrian agreed and thanks a lot.
But if there are couple of processes with the same name (of course with different process ID) how to identify the process which I want to kill?
In fact my actual requirement is I have an application which required couple of predefined ports to run.Individual element of that application runs on different ports.We can start those individual elements alone if required.But afterthat if I tried to start the application it throws an error saying that the port required to run the application is occupied.So I wantt o write a shellprogramme to get execute prior to start that application which kills all the processes which runs on those ports.

What I feel is there should be a mechanism to relate the processID with the port in which it runs.If we can do that one then .
rgds
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Maybe you can attack this problem from another direction. I don't have the right manuals with me now, but I recall seeing a way of setting the text that appears in ps from your process. It's usually used to make sure that command-line passwords etc. don't apear in the ps listing, but in your case you could probably build a name which includes a "unique" string and the port number. Then your grep and kill approach would always hit just the right processes.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
ram mohan
Ranch Hand

Joined: Oct 17, 2000
Posts: 68
Thanks frank for the valuable idea.
I will work on it
vaibhav waghmare
Greenhorn

Joined: May 16, 2001
Posts: 15
hi,
i'm a new user to linux. so tell me whats the harm in killing a process using the command :
kill and option like -9 followed by the PID ???
ne sugestions ???


------------------
love n regards,
-VaibhaV.


love n regards,<P>-VaibhaV.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
The problem with this is that you don't usually know the PID. Particularly if your program is written in Java, where you have no way of finding out the PID from the running program, or if you have multiple copies of the same application running. Imagine I run the following:

when I run "ps" to see what is running I get something like:

Now, remind me again which PID I should kill to stop the one listening to port 8005 ?
vaibhav waghmare
Greenhorn

Joined: May 16, 2001
Posts: 15
HI FRANK,
thanx for the practical and precise answer. i'd happy if somebody like YOU can reply to one of my pending post. no body has replied it yet
hope i'll get the help !!!
------------------
love n regards,
-VaibhaV.
Patrik Jarnefelt
Greenhorn

Joined: May 21, 2001
Posts: 1
Originally posted by Frank Carver:
Now, remind me again which PID I should kill to stop the one listening to port 8005 ?

short answer: netstat.
long answer:
Ok, I have tomcat running on my computer..
ps auxww |grep tomcat
root 905 0.1 7.8 91636 20488 pts/1 S 09:37 0:28 /usr/local/jdk1.2.2/bin/i386/green_threads/java -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/jdk1.2.2/lib/tools.jar -Dcatalina.home=/usr/local/tomcat org.apache.catalina.startup.Bootstrap start
So we would like to kill pid 905. But the problem was finding this pid when we know what port it's using. So we take a look at netstat.
netstat -anp | grep :8005
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:8005 0.0.0.0:* LISTEN 905/java
Great. Pid 905 is listening on 8005. That's want we wanted to know. Extracting the pid (for scripts) could be done in a million ways, here's one:
netstat -anp 2>/dev/null | grep :8005 | awk '{print substr($7, 0, index($7, "/") - 1)}'
905
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Cool. Thanks Patrik, that's not something I had come across before but it sure seems to do the job.
hennie louw
Ranch Hand

Joined: Jul 03, 2001
Posts: 56
What You could do to kill this process is have all the instances of you application look for a file, this file can be any where, once it finds this file, it go's into it's shut down methods.
(but this way all the instances of you app will shut down once the file is created) thus if you know on witch port the instance is you want to shut down you can write that into the file and have you app read the contents of the file and if is matches the instances port then it should shut down and delete the file. (One thing here, you could create a lock file that must also be checked. if it exsists don't delete until the locke file is gone.), this way you dont have to worry about an error if an other instance is reading the file when you try to delete it.
Eg. you want to shut down instance listening on port 9085 and the "'shutdown" file should be /etc/shutdown.ME then to shut down the instance listening on port 9085 you would just do a
echo 9085 > /etc/shutdown.ME , and it should sut down.
=================================================
Also you could make each instance register it self with a global manager, and have the manager contain
info on the port that the instance listens to, and the via a command in eg telnet localhost, you could
shut down the instance running on port 9085
This would is not the best way to do it, but is seems to be OS independant way


Any Body can be paid to write good code, but brilliant code can only come from passion
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by vaibhav waghmare:

whats the harm in killing a process using the command :
kill and option like -9 followed by the PID ???

The kill command relies on the Unix concept of signals, which are messages directed to a process from the the kernel. Since the kernel owns and operates all the processes in its space, it is free to invoke a signal on any process at any time.
Most signals represent a potential interruption to the program's operation. You can probably run kill -l on your version of Linux to get a listing of signal types. Alternatively, you can look at the header file signal.h.
The "-9" option to kill is more appropriately called the KILL signal (try kill -l 9). Its purpose is to shut down a process immediately, regardless of its current state. That is, the program will have no opportunity to gracefully close any open files or quiesce other data. This means it's possible to corrupt program state or lose data.
So, two things: one, don't use a KILL signal when a TERM (terminate) signal will do the job; two, best practice suggest using the mnemonic instead of the number. In the highly unlikely (but possible) situation that someone changes the integers values in their kernel code, the mnemonics are supposed to retain their named behavior.
TERM is usually assigned the integer value 15.
Finally, with daemon services that you want to interrupt so that they will startup and reread their configuration files, use kill -HUP (aka integer value 1).
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Other notes; there is a tool floating around called lsof that helps you map a port number in use back to the process that grabbed it. Linux may have something similar by a different name; me being a Solaris/AIX/HP-UX/SCO hacker with no interest in yet-another-Unix, I couldn't tell you.
For extra credit: the usual keyboard interruptes Ctrl-C and Ctrl-D map to their own interrupt numbers too, usually 2 (INT) and 3 (QUIT).
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I'm having the same problem as the original poster; I've got several java programs running, and I can't tell which one is which in order to kill *one* of them.
I've read through this thread, but I'm still not sure what's the *easiest* way to handle the problem.
I like the netstat idea, but if the program is not a server then I'm not sure that I can get the pid from netstat.
Is there a way to label a java program on the command line, so that I could kill it with pkill?
Drew
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

The "AT&T" implementation of ps has a switch that outputs more than 80 columns of process entry information. You'd invoke it as: ps -auxwww to get "wide wide wide" output.
It's an arcane vestige of ps but has some good uses for Java programs. Hopefully someone thought to include it in the Linux version of ps.
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Hi - On Linux, if you are running native threads, and you do:
java pgma
and then, other console, you do:
ps axw | grep pgma
you will see 8-9 processes running for that java command. To kill 'em all, do:
ps axw | grep pgma | sed -e '/grep/d' | awk '{print $1}' | xargs kill

Another approach is to do:
java pgma &
you will get the parent pid in a message as the BG job is kicked off, then kill it if you need to.
Guy
As Michael suggested you might have to use some extra ws, god help us all, we got one too many of them.
[ November 18, 2002: Message edited by: Guy Allard ]
[ November 18, 2002: Message edited by: Guy Allard ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to kill a process(I know the port in which it runs)