54

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 ~]$
d-man
  • 57,473
  • 85
  • 212
  • 296

5 Answers5

111

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'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Yes you can add jar name as well. – anubhava Dec 04 '12 at 21:15
  • 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' ? – d-man Dec 05 '12 at 16:27
  • You can use: `pgrep -if 'java .*-jar'` – anubhava Dec 05 '12 at 16:35
  • but when i tried pkill -f pgrep -f 'java .*-jar' it didn't work... gives help option – d-man Dec 05 '12 at 16:42
  • You can simply do: `kill $(pgrep -f 'java .*-jar')` – anubhava Dec 05 '12 at 16:44
  • this also works kill -9 `ps -ef | pgrep -f 'java .*-jar'` but i like 'anubhava's ans it is simple. – d-man Dec 05 '12 at 16:47
  • i am sorry but my jar name is 'lnwskInterface' and there may be more then one jars running i want to kill only my jar i tried "kill $(pgrep -f 'java .*lnwskInterface-jar')" can you help me pls – d-man Dec 05 '12 at 16:51
  • I don't get what's the problem, even `pkill -f 'java.*lnwskInterface'` should work. – anubhava Dec 05 '12 at 16:57
  • 1
    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 – d-man Dec 05 '12 at 17:10
  • 1
    try double quotes instead like this: `pkill -f "java.*$app"` – anubhava Dec 05 '12 at 17:32
  • @anubhava - I'm killing java process using shell_exec('kill -9 $pid') but I want to set time limit here like this, $pid process should be killed automatically after 1 minute. So, how can I set this time constraint here?? – hiren panchal Mar 01 '14 at 06:50
  • You can probably use: `sleep 60; kill -9 $pid` – anubhava Mar 01 '14 at 13:30
  • pkill -f 'java -jar nameSpecific.jar' – iarroyo Oct 21 '15 at 09:36
  • How is it different from `pkill java`? – Foreever Jan 25 '21 at 17:55
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...

lynks
  • 5,599
  • 6
  • 23
  • 42
  • 1
    I'm always amazed to see `grep`s piped into `awk`s!!! 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). – gniourf_gniourf Dec 04 '12 at 21:13
  • 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 – d-man Dec 04 '12 at 21:15
  • @Faisalkhan That's because you tried to kill the `ps` process itself... `:-(`. Don't use this method, use anubhava's instead. – gniourf_gniourf Dec 04 '12 at 21:17
  • @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. – lynks Dec 04 '12 at 21:21
  • 1
    `ps -ef | grep wskInterface | grep -v grep | awk '{ print $2 }'` ignores your own grep command. – Fırat Küçük Jun 30 '15 at 11:18
4

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.

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
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

Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46
0

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.

Pawan
  • 1,183
  • 16
  • 29