56

I want linux script to kill java program running on console.

Following is the process running as jar.

[rapp@s1-dlap0 ~]$ ps -ef |grep java
rapp    9473    1  0 15:03 pts/1    00:00:15 java -jar wskInterface-0.0.1-SNAPSHOT-jar-with-dependencies.jar
rapp   10177  8995  0 16:00 pts/1    00:00:00 grep java
[rapp@s1-dlap0 ~]$
1
  • 5
    does pkill java count? Commented Dec 4, 2012 at 21:04

5 Answers 5

113

You can simply use pkill -f like this:

pkill -f 'java -jar'

EDIT: To kill a particular java process running your specific jar use this regex based pkill command:

pkill -f 'java.*lnwskInterface'
Sign up to request clarification or add additional context in comments.

14 Comments

Yes you can add jar name as well.
now my jar in ps looks like rcapp 23671 1 16 11:23 pts/0 00:00:03 /usr/java/jdk1.6.0_37//bin/java -Xmx2048m -Drdc.log.file=/home/rapp/apps/LNWSKInterface/logs/dev.log -jar /home/rapp/apps/LNWSKInterface/jar/lnwskInterface.jar and if i do pkill -f 'java -jar' it does not work. can i use wild card like as follows pkill -f 'java *-jar' ?
You can use: pgrep -if 'java .*-jar'
you are very helpful i am grateful to you, have one more last question i have export variable $app=lnwskInterface when i tried pkill -f 'java.*$app' didn't work out. thanks for help in advance
try double quotes instead like this: pkill -f "java.*$app"
|
44

If you just want to kill any/all java processes, then all you need is;

killall java

If, however, you want to kill the wskInterface process in particular, then you're most of the way there, you just need to strip out the process id;

PID=`ps -ef | grep wskInterface | awk '{ print $2 }'`
kill -9 $PID

Should do it, there is probably an easier way though...

5 Comments

I'm always amazed to see greps piped into awks!!! Shall we call this Useless Use Of Grep? Anyways, it's much better to use pkill with the -f option as anubhava mentioned (if you have pkill installed of course, and if it has the -f option).
I tried "PID=ps -ef | grep wskInterface | awk '{ print $2 }' kill -9 $PID" and found -bash: kill: (10395) - No such process. it actually killed the process but also give the error
@Faisalkhan That's because you tried to kill the ps process itself... :-(. Don't use this method, use anubhava's instead.
@gniourf_gniourf I find grep to be a little easier when it comes to filtering out lines. And yep, I knew this method was a little off when I typed it, but it should work. And it's beardy, which I enjoy.
ps -ef | grep wskInterface | grep -v grep | awk '{ print $2 }' ignores your own grep command.
5

if there are multiple java processes and you wish to kill them with one command try the below command

kill -9 $(ps -ef | pgrep -f "java")

replace "java" with any process string identifier , to kill anything else.

Comments

1

Use jps to list running java processes. The command returns the process id along with the main class. You can use kill command to kill the process with the returned id or use following one liner script.

kill $(jps | grep <MainClass> | awk '{print $1}')

MainClass is a class in your running java program which contains the main method.

2 Comments

@Xerus Try kill $(jps -l | grep <fully.qualified.ClassName> | awk '{print $1}')
the issue seems to be the grep. Either of your suggestions only work once I take out the grep
1

pkill -f for whatever reason does not work for me. Whatever that does, it seems very finicky about actually grepping through what ps aux shows me clearly is there.

After an afternoon of swearing I went for putting the following in my start script:

(ps aux | grep -v -e 'grep ' | grep MainApp | tr -s " " | cut -d " " -f 2 | xargs kill -9 ) || true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.