Stop current running Spring boot in linux - java

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.

Related

How to kill a process which is running form a directory in CentOS?

We have a requirement where we have copies of the same Spark application (basically a .jar file) running from different folders. I am looking for a shell command using which I can kill the Spark app running in one folder without killing other spark jobs.
Ex:
/home/user/Folder1 - CapStoneSpark.jar
/home/user/Folder2 - CapStoneSpark.jar
/home/user/Folder3 - CapStoneSpark.jar
The main class in the jar file is "CapStoneSparkMain". Suppose, I want to kill the CapStoneSpark.jar running in Folder2 only without touching or killing the CapStoneSpark.jar running from Folder1 and Folder3, then what should I do?
I have already tried:
kill $(ps aux | grep 'CapStoneSparkMain' | awk '{print $2}')
But, it kills all the process which have "CapStoneSparkMain" class in them.
I only want to kill the process originating from a single directory and don't want to touch the copies of the processes running from other directories.
You can find all Proccess ID's which using these folders:
lsof | grep /home/user/Folder | awk '{print "kill " $2}'
And execute it:
lsof | grep /home/user/Folder | awk '{print "kill " $2}'|sh
It's not clear how the jobs are started, but assuming that each job is started with a different working direectory, it is possible kill the specific job. Given that it's possible to find the working directory of each process via the /proc/$$/cwd (symlink to the job folder). Building on the commands/loop suggested above:
kill_folder=/home/user/Folder2
for pp in $(ps aux | grep 'CapStoneSparkMain' | awk '{print $2}') ; do
if [[ /proc/$$/cwd -ef "$kill_folder" ]] && kill $$
done
The code will check if the symlink /proc/$$/cwd matches the named folder (kill_folder), and will only issue the kill to processes in that folder.
Hello
A for loop that check for the user in...
for i in $(pidof application); do grep -o ${user} /proc/${i}/cmdline; done
And if it fits your desire then put kill command on ${i} in loop.

War file automated deployment

So I deploy war files on a Linux box using java automatically
and the same commands are always used
ps -ef | grep java
kill - 9 (java process)
java -jar ROOT.war &>/dev/null &
However, I get different versions for it so like
ROOT_1.0.2.war
ROOT_1.0.3.war
ROOT_1.0.4.war
ROOT_1.0.5.war
I want the script to see the new .war and deploy it automatically
and keep it deployed which is why I use &>/dev/null & so it runs in the till it is killed again till the new version is put in that directory
echo Enter the name of the process you want to kill eg ROOT.war?
enter code here
read process
##Kill selected process
file="$process"
if [ -f "$file" ];
then
pkill -9 -f $process
echo process stopped >> satrixWar.txt
sleep 3s
## Start Up selected process
echo Enter the name of the process you want to start
read process2
java -jar $process2 &>/dev/null &
echo process starting up>> satrixWar.txt
else
echo "Process $process does not exist" >&2
fi
##Confirm new proess is up
echo What is currently installed >> satrixWar.txt
ps -ef | grep $process2 >> satrixWar.txt
mail -a text file path -s "name"
"email.com" < /dev/null
rm -rf War.txt

Kill Java Thread In Linux

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

Stopping an unknown process in linux server

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.

How to create a script to restart a JAR file and get output in a terminal?

I have used the following scripting for start and stop a jar file.
**start.sh**
#!/bin/bash
nohup nice java -jar Server.jar > ./Server.out 2>&1 &
**stop.sh**
#!/bin/bash
kill `ps -ef | grep Server.jar | grep -v grep | awk '{ print $2 }'`
Now I want to merge both scripts and create a new restart script. I also want this script output in a terminal instead of a text file(Server.out).
Would appreciate any kind of input/help.
You can either put the commands of the two sripts after each other (kill first, java second) or just call the two scipts in the appropriate order.
The idea is that restart is basically equivalent to killing the current running version and starting a new one.
To avoid the output to a file, remove the > ./Server.out part.
Edit: removed note about removing the redirection part as I misread the grep part of the kill script
Update: Missed the nohup part of the script: with nohup you need to redirect output to a file, because the process is detached from the terminal (see documentation). If you do want to see the output in the terminal, remove nohup as well as the redirection to the file

Categories