Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux script to kill java process

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 ~]$ 
like image 745
d-man Avatar asked Dec 04 '12 21:12

d-man


People also ask

How do you kill a Java process?

Usual taskkill /im "java.exe" will kill all java processes in the list.

How do I stop Java from running on Linux?

You can use kill command to kill the process with the returned id or use following one liner script. MainClass is a class in your running java program which contains the main method.

How do I stop and start Java in Linux?

Here is the command in full: ps -fC java . You could also use pgrep to list all java processes. pgrep -a java will return the PID and full command line of each java process. Once you have the PID of the command you wish to kill, use kill with the -9 (SIGKILL) flag and the PID of the java process you wish to kill.


2 Answers

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' 
like image 179
anubhava Avatar answered Sep 19 '22 18:09

anubhava


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...

like image 36
lynks Avatar answered Sep 19 '22 18:09

lynks