In Linux Server, I have One Java Application...
Sometimes Its Thread Gone Stuck..
How Can I Find Them And Kill That Particular Thread of Java.
I want to kill that java thread who takes more then 5 minute of time
Top -H result as image
You can't only kill a thread of a process, if you use the command "kill -9 threadNo", you will kill the process.
Find java process using below
$ ps -fea | grep -i java
Here is the sample output
user 2895 8191 0 09:28 pts/1 00:00:00 grep -i java
user 4610 4607 1 Aug29 ? 01:40:00 /home/user/Software/java-7-sun/bin/java -Dosgi.requiredJavaVersion=1.6 -Xms40m -Xmx768m -XX:MaxPermSize=256m -jar /home/user/Software/springsource/sts-3.3.0.RELEASE//plugins/org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar
and then kill the process
$ kill -9 4610
As you want to automate this via Shell, to get pid, further search in the text with your specific java app (probably search location of app)
ps -ef | grep java | grep -v grep
Extract PID portion using SED
Related
i am running a springboot application in linux.To run this springboot we use below command.
java -jar sssup-SNAPSHOT.jar &
This spring boot application makes an endpoint available which is then used by other services.
Now when the new version of .jar is available i have to stop the current running .jar and again run the above mentioned command.
Here my question is how to stop the current running sssup-SNAPSHOT.jar ?
Since you are running the process in the background you need to find its process first to kill it.
To find the process ID fire below command:
ps -ef | grep "sssup-SNAPSHOT.jar"
The output will look something like below:
502 5980 5964 0 10:40AM ttys000
Your second column is your process ID So, over here it is 5980. After copying that fire below command.
kill 5980
Just check again by firing ps -ef command that it gets killed or not. If it's not killed already than you can use -9 flag to force kill it. Like kill -9 <PID>
One way is to bring the background process to the foreground with the fg command and then press Ctrl+C. This will only work if you are in the same terminal where you ran the JAR file.
Another more reliable way is to use ps -a | grep java to find the process id of your app. Then you can do kill <PID> with that process id.
If you want to do maually then do below steps
1. open terminal
2. type JPS and enter
3. copy your application process id
4. sudo kill -9 application_id
If JPS is not avilable then
1. open terminal
2. type ps -a | grep sssup-SNAPSHOT.jar
3. copy you process id
4. do sudo kill -9 processid
If you want to do via Shell script use below
app_id=$(ps -ef | grep 'sssup-SNAPSHOT.jar' | grep -v 'grep' | awk '{ printf $2 }')
sudo kill $app_id
echo Killed Application and Starting New
nohup java -jar "sssup-SNAPSHOT.jar" &
You kind find the PID of the spring process and send a SIGTERM signal to it.
Find the PID (Here the PID is 12345)
$ ps -ef | grep sssup | awk '{print $2}'
12345
Send a kill signal (This will send the signal SIGTERM)
kill 12345
If the application handles the SIGTERM gracefully, you can force kill it by sending SIGKILL
kill -s SIGKILL 12345
### or
kill -9 12345
Signals are documented here
https://www.man7.org/linux/man-pages/man7/signal.7.html
You can list signals and their numbers by running
kill -l
EDIT: Onliner
ps -ef | grep "sssup-SNAPSHO[T]" | awk '{print $2}' | xargs kill
Spring boot print's the process id in the log (Number after INFO in log) then use command kill -9 pid
Note: 9 means the process will be killed by the kernel; this signal cannot be ignored.
I have this command in my deployment process. But I want this process to stop and then restart while deploying in linux server. I checked and found that this is simply a JAVA process, and I can't simply kill JAVA as other nimbus and supervisors are running too.
sudo nohup java -Droute=full -jar /opt/artifacts/project/project.jar --spring.config.location=/etc/project/application_full.properties >/dev/null 2>&1 &
So, how can I stop this process?
Your oneliner kill: (I know it's bad, but it should work)
ps -ef| grep "name_of_service" | grep -oP "root\s+(\d+)\s" | grep -oP "\d+" | kill
ps -ef finds the program line:
root 5727 1 0 11:38 ? 00:00:00 grep service
Then we use grep to remove parts we don't want.
And lastly pass the pid to kill.
ps: replace 'root' for the user you know run the service/pid you are looking for.
I am trying to use a script to start a nohup command. I have written a test program in Java to launch via the script, TestScript.java. My script contains the following:
#!/bin/bash
cd /home/user/ejava
nohup java TestScript > TestScript.out &
echo $! > /home/user/ejava/TestScript.pid
I use echo $! to get the last process ID and store it to a temp file. When I look in the TestScript.pid, the value is 37458.
When I run a 'ps -elf | grep TestScript', I get:
0 S user 37458 1 0 80 0 - 1065 rt_sig 11:51 pts/0 00:00:00 /bin/ksh /folder/tools/Linux_x86_64/bin/java TestScript
0 S user 37463 37458 0 80 0 - 8813549 futex_ 11:51 pts/0 00:00:00 /folder/tools/Linux_x86_64/bin/../java/jre_Linux_x86_64/bin/java TestScript
0 S user 37516 36224 0 80 0 - 1595 - 11:52 pts/0 00:00:00 grep TestScript
If I kill job 37458, then 37463 is still running and my output from the nohup is still being updated. The job is not killed.
How can I get the process id 37463 returned (or whatever correlates to the command with futex in it) so I can store it in a file and later use it to kill the process with another script? I would prefer not to have to search for the process ID but rather get it returned by some process.
If you start your process in the background using the ampersand character at the end of your command then the shell variable ! is populated with the pid you are looking for:
john#ubuntu:~$ nohup java -cp . Tst > tst.out 2>&1 &
[1] 6901
john#ubuntu:~$ echo $!
6901
john#ubuntu:~$ ps -ef | grep java
john 6901 4844 0 09:27 pts/12 00:00:00 java -cp . Tst
john 6913 4844 0 09:27 pts/12 00:00:00 grep --color=auto java
john#ubuntu:~$
I found it here.
Our server machine runs many java programes. And some of them are launched with the command "java -jar ***.jar". but sometimes I have to stop one to update the class files in it. the problem is how can i fingure out which program is the one i want to stop, or is there a tool I can use to find out the executable jar files' location.
You can use jps command.
$ jps -v
34370 Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home -Xms8m
34341 main -XX:+TieredCompilation -Xbootclasspath/a:/usr/local/Cellar/leiningen/2.5.0/libexec/leiningen-2.5.0-standalone.jar -Dfile.encoding=UTF-8 -Dmaven.wagon.http.ssl.easy=false -Dleiningen.original.pwd=/Users/ntalbs/js-workspace/synapeditor_mobile -Dleiningen.script=/usr/local/bin/lein
jps will display all java processes. The first column is OS pid. You can check the messages on console, then kill what you want. Perhaps you want to check jps document from Oracle.
Also, you can use ps and grep command.
$ ps -ef | grep java
or
$ ps -aux | grep java
I'm running Minecraft under Linux, which involves running an executable .jar file. This means it shows up as "java" under ps, rather than "minecraft". I would like to assign it the process name "minecraft".
Looking around, I found the following tip for assigning a process name via bash:
how to change the name of a Java application process?
exec -a goodname java ...
I usually run with:
java -cp ~/Games/Minecraft/Minecraft.jar net.minecraft.LauncherFrame
So tried make a bash script:
#!/bin/bash
exec -a minecraft java -cp ~/Games/Minecraft/Minecraft.jar net.minecraft.LauncherFrame
But when I run this, it still shows up as "java" under the ps command.
What am I doing wrong?
It works for me. I haven't tested with java, but I tested with sleep:
victor#vz:~$ exec -a minecraft sleep 1m &
[1] 3858
victor#vz:~$ ps x | grep mine
3858 pts/2 S 0:00 minecraft 1m
3860 pts/2 S+ 0:00 grep --color=auto mine
victor#vz:~$
However, this seems to be merely a cosmetic change as far as I can tell by the documentation:
victor#vz:~$ help exec exec: exec
[-cl] [-a name] [command [arguments
...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
Options:
-a name pass NAME as the zeroth argument to COMMAND
In reference to OP's comment to this answer: I just tested it on a remote machine with java as well:
victorz#exa:~$ javac test.java # spits out an Administrator.class file among others
victorz#exa:~$ exec -a minecraft java Administrator &
[1] 13142
victorz#exa:~$ ps x | grep mine
13142 pts/1 Sl 0:00 minecraft Administrator
13161 pts/1 S+ 0:00 grep --color=auto mine
victorz#exa:~$
Maybe you are not using the x switch to ps? I get no match unless I use the x switch.