kill a java process in Linux - java

I have a BI application (looker) runs on a linux VM.
tobe able to restart the service, I need to clear the existing java process.
In below screenshot, after run below script, there is a java process, but not showing in the list when I run jps script. What's the reason? and how can I properly terminate this java process?
ps aux | grep java

Have you tried these ?
killall java
or
kill $(pidof java)

As you can see from your image, the process id is changing each time 9287 / 9304 and represents | grep java - and not a java VM!
A common fix is to filter the ps results for not matching | grep -v, such as:
ps aux | grep java | grep -v --regexp=grep.\*java
If there are results above you could append commands to read the process ids and kill command:
kill -TERM $(ps aux | grep java | grep -v --regexp=grep.\*java | awk '{print $2}')
Note: the above will kill all processes with "java" in name so is not very useful if there are multiple java services for same account. You may need to add filter for specific Java VMs.

Related

How to stop the process from running?

This question might be a bit similar to this or this question.
How do I stop a particular .jar file from running in Mac OS? When I open up the activity monitor, it does not show the process listed. But I am sure it is running because when I visit the localhost (I have developed a .jar from a Spring Boot application), I can still see the welcome message!
Could you please let me know how I could stop a particular .jar file from executing? Thanks.
You can use jps to get the pid (and jar name), awk to parse the pid and then kill it. Like, (with bash or similar)
kill -9 $(jps | grep -i "thejar.jar" | awk '{print $1}')
or
kill -9 `jps | grep -i "thejar.jar" | awk '{print $1}'`
The -i option to grep makes it case insensitive. Omit if that is not needed.
Step1: ps -aux | grep xxx, xxx is the name of .jar
Step2: kill -9 pid, pid you can get from the first command.

Getting PID of a process that has just started

I am working on a Gradle Java project. Which starts Tomcat for testing and stops it later.
I am supposed to kill this Tomcat instance when the test fails.
I tried using "ps aux| grep tomcat | grep -v grap | awk {print $2}" command to get the process id and kill the process.
But on Production, there will be so many Tomcat processes running simultaneously by many users, I just want the tomcat process started by my build.gradle for test execution.
So how can I accomplish the task? Please provide me some guidelines.
You need to find a unique string in the output of 'ps aux' which differentiates your test tomcat and others'.
I currently use the below script to run 'shutdown.sh' first and then kill the PID as most of the times, the application stops but the process does not stop.
PID=`ps -ef | grep $JAVA_HOME/bin/java | grep "$TOMCAT_LOC"/conf | grep -v grep | awk '{ print $2 }'`
if [ $PID ]; then
echo tomcat is running with PID:$PID.
# Stop or Kill running Tomcat
if [[ -f $TOMCAT_LOC/bin/shutdown.sh ]]; then
[[ ! -x $TOMCAT_LOC/bin/shutdown.sh ]] && chmod a+x $TOMCAT_LOC/bin/shutdown.sh
$TOMCAT_LOC/bin/shutdown.sh >>/dev/null
sleep 20
fi
kill -9 $PID
sleep 3
else
echo tomcat is not running
fi
You may also look at configuring a PID file by editing the 'catalina.sh' which you can read later to find out your PID.
# CATALINA_PID (Optional) Path of the file which should contains the pid
# of the catalina startup java process, when start (fork) is
# used
Java JRE has tool called jps in $JAVA_HOME/bin folder.
It's similar to unix ps command but for java only.
You can use it to determined exac java process you need.
Using this tool is more recommended and actually it is more useful, when you have more than one java applications is running on your host...
for example I have running h2 database and many other apps, but wanna kill only h2, so I can use jps to get it PID
$ jps
17810 GradleDaemon
17798 GradleWrapperMain
17816 h2-1.4.197.jar
17817 GradleDaemon
17818 GradleDaemon
18011 Jps
16479
and then just kill needed process:
kill -9 17816
and all other java apps will continue work normally. I not sure about tomcat, but I think it can be done in similar way, something like that:
kill -9 $(jps | grep tomcat | awk '{print $1}')
Lastly, little bit offtopic, but a specially to your case: correct way would be using start/stop/restart scripts provided by tomcat
The correct way to terminate a Tomcat instance is via its own shutdown command. You should not be thinking of processes, or PIDs, or kills, at all.
so if you want to kill tomcat from that user from which you have logged in then try following and let me know if this helps you.
ps -ef | grep -v grep | grep `whoami` | grep tomcat
So by ps -ef I am listing all the processes then grep -v grep will remove the grep command's process then grep whoami will look for your currently logged in user then grep tomcat will look only for tomcat process, test it once and if All is Well then you could kill it.
By the way how about tomcat stop script? In case it is there you could use that also.
You can use shell variable $!. It represents the PID of the most recent background command.
yourCommand &
CMD_PID=$!
echo $CMD_PID

Shell script to take thread dump of a java process

With jstack we can take thread dump of a running java process. With top and ps -aef | grep java commands, we can identify the rogue process using most of the system resources.
So, if we use jstack <rogue pid> >> threaddump.log we can take the thread dump of the specified java process.
Now my question, is there any shell script that can identify the top java process, take process id(pid), inputs to jstack utility and take the thread dump?
I've searched online and most of the links need manual efforts of inputting process id. So, I'm just curios to know if there is any existing shell script which can avoid manual input.
Thanks in advance.
For me this pipe works:
ps -eo pid,%cpu,comm | grep java |sort -nr -k2 | head -n1 | awk '{print $1}' | xargs jstack
Explanation:
ps -eo pid,%cpu,comm : prints all processes with PID CPU usage and command name
grep java : greps all java processes
sort -nr -k2 : sorts the result numerical reverse by the second column
head -n1 : prints the first row
awk '{print $1}' : prints the first column
xargs jstack : takes the input and uses it as argument for the jstack command

How to get PID and Port # for a Jenkins Process

I need to get PID & Port# for a Jenkins process run. If i get that PID, i can kill the process if ever i need to.
I am running the Jenkins process by below commands:
java -jar jenkins.war
Sometimes, Jenkins Process fail to start if that port is taken and below occurs:
Jenkins home directory: /Users/MacPro/.jenkins found at: $user.home/.jenkins
Feb 27, 2016 10:46:09 AM org.eclipse.jetty.util.log.JavaUtilLog warn
WARNING: FAILED
SelectChannelConnector#0.0.0.0:8080:java.net.BindException:
Address already in use
java.net.BindException: Address already in use
And I know how to run the jenkins process against a specific Port#.
Need to know the commands for which PID and the port, current job is using.
The command will be below:
ps -ef| grep jenkins
It will display the process id.
This should do the job. Better to find and kill using the same command, saves time:
ps -Af | grep "jenkins" | grep -v grep | awk '{print$2}' | xargs kill -9
You can check the process before killing
ps -Af | grep "jenkins" | grep -v grep | awk '{print$2}'
If you are running Jenkins using tomcat
ps -Af | grep "tomcat" | grep -v grep | awk '{print$2}' | xargs kill -9
Please note that these commands tested on RHEL.
Answer to your question
1) In Unix box, the command usage will be ps -ef| grep jenkins, it will display the process id (pid)
2) kill -9 (pid)
You could try the following command for a cleaner output:
lsof -Pni | grep mysql

How to capture the result of a system call in a shell variable?

We want to build a script that run every night (kills and restart a java process). For that we need to capture the process number (since there could be more than one java process running). The command below is basically what we will use to obtain the processes number, probably with a regexp at the end of the grep. Unless any better suggestions comes up.
root#ps -e |grep 'java'
18179 pts/0 00:00:43 java
We want to know how to parse the output above and get it into a shell variable so we can use the kill command as below.
kill -9 ${processid}
wait 10
Note1: The reason we cannot rely on the normal service stop command is because the processes sometimes does not want to die. And we have to use the kill command manually.
There are a couple of options to solve this. If you're using bash, then the shell variable '$!' will contain the PID of the last forked-off child process. So, after you start your Java program, do something like:
echo $! > /var/run/my-process.pid
Then, after your init script stops the Java process:
# Get the pidfile.
pid=$(cat /var/run/my-process.pid)
# Wait ten seconds to stop our process.
for count in $(1 2 3 4 5 6 7 8 9 10); do
sleep 1
cat "/proc/$pid/cmdline" 2>/dev/null | grep -q java
test $? -ne 0 && pid="" && break
done
# If we haven't stopped, kill the process.
if [ ! -z "$pid" ]; then
echo "Not stopping; terminating with extreme prejudice."
kill -9 $pid
fi
Make sure to remove the pidfile when you're done.
ps aux | grep java | awk '{print $1}' | xargs kill -9
Here's an explanation:
ps aux gives you a listing of all processes
grep java gives you all of the processes whose names and command line arguments contain the string "java"
awk '{print $1}' parses the output of the grep command into columns by whitespace and re-prints only the first column
xargs kill -9 passes each of the results of the awk command as parameters to a kill -9 command
I realize this is old, but what about:
pidof java | xargs kill
You can easily get the PID or list of PIDs into a variable using backticks and cut (or awk if preferred) to retrieve only the PID field:
[user#host ~]$ ps -e | grep java | cut -d' ' -f1
12812
12870
13008
13042
13060
Note in the above example I have multiple Java processes running hence the multiple values. If you save this into a variable like so:
JAVA_PROCS=`ps -e | grep java | cut -d' ' -f1`
You can iterate through the processes to kill them if desired:
for proc in $JAVA_PROCS; do
kill -9 $proc;
done
Of course, if you're only retrieving one process, then there's no need to iterate and you can just run it as:
kill -9 $JAVA_PROCS
If you do what you suggest, you may end up capturing the grep itself and killing that (since your grep command contains the java string that you are searching for). You can work around this by excluding grep (by using another grep!):
pid=`ps -e | fgrep java | fgrep -v grep | awk '{print $1}'`
# check pid has a value
# kill $pid
You might also like ps -e -opid,args.
A better alternative is to use pgrep(1) or pkill(1) if your system has them. No more pipes, seds, awks, cuts, xargs:
pkill -9 java
I use something like this:
kill $(ps -A | grep java | cut -b 1-5)
killing it:
ps -e | grep java | cut -f1 -d' ' | xargs kill -9
storing PID on variable:
export JAVAPID=`ps -e | grep 'java' | cut -f1 -d' '`
checking that it worked:
echo $JAVAPID

Categories

Resources