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 ~]$
Usual taskkill /im "java.exe" will kill all java processes in the list.
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.
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.
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'
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With