Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have to start some performance test on a Rhel 6 box, but before starting the test, i have to make sure there are no other java processes running.

The manual way of doing it would be to use "ps -ef | grep java"

But, since this test is going to be automated, i need a way to programmatically detect it and kill all of them.

Is there some way of doing it?

share|improve this question
1  
If you happen to search for processes, which you don't want to kill, just use ps -C java instead of grep. –  user unknown Jan 25 '12 at 10:25
add comment

3 Answers

up vote 5 down vote accepted

How about...?

pkill java

pkill(1) (also killall(1) on Linux) works like kill(1), but sends the signal to all the processes with the specified name(s), not PIDs. There are other ways of matching processes too — check the man page!

In fact, specifically because this is Java we're talking about, I'd do the following to automate things properly:

pgrep java >/dev/null && (pkill java; sleep 5)
pgrep java >/dev/null && (pkill -9 java; sleep 5)
if pgrep java >/dev/null; then
    echo "Whoops, java processes still running. Bailing out."
    exit 1
fi

This will check if any java processes are running, send the, the SIGTERM signal and wait 5 seconds. If there are still processes running, it sends the SIGKILL signal and waits another 5 seconds. If there are still processes running, it stops executing the script returning an exit code of 1.

Update: changed answer to use pgrep(1) and pkill(1) instead of pidof(1) and killall(1).

share|improve this answer
2  
Caveat emptor! killall on Linux is special. killall on many other Unices sends a signal to all processes, not all processes matching a particular pattern. –  Alexios Jan 24 '12 at 20:16
2  
It is CentOS we are talking about here. But any way pkill should do the job as well - and is propably secure on any Unix that sports that command (even OpenBSD does). –  Nils Jan 24 '12 at 21:39
    
Indeed! (I just like to play it safe and mention this for other people reading the answer, people who might not know this). And I learned something new today. :) pkill(1) is also available on Solaris, so it's obviously the better way to do this. –  Alexios Jan 24 '12 at 22:53
    
In fact, I think I'll modify my answer to use pkill. That should make it compatible with most Unices. –  Alexios Jan 24 '12 at 22:58
    
php ----- system("pkill java"); this line not working . i dono wat may be the problem. Can u help me. thanks –  user19351 May 30 '12 at 7:22
add comment

It should be very simple with pkill:

pkill java
share|improve this answer
add comment

First, write output of ps -ef | grep java to a file named "filename" then create a Python file:

import os

f = open('filename',r')`
for i in f:
    a = i.split()
    b= "kill "
    b=b+str(a[1])
    os.system(b)

Run it with python pythonfilename.py and your Java programs will be terminated.

share|improve this answer
1  
You may just use ps -C java instead of grepping the output of ps, but this solution is so overly complicated ... –  user unknown Jan 25 '12 at 10:28
    
@user unknown: I just followed his procedure. sorry I usually do this using python .I was not aware of pkill –  harish.venkat Jan 25 '12 at 14:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.