Use shell script to kill specified port process

Before killing the process, let everyone learn two simple Linux commands

1. View the specified port process
netstat -lnp|grep 8089		// 这里是查看端口为8089的进程
2. Kill the pid of the specified process
kill -9 32741		// 这里的32741是通过查看进程知道的,每个进程有个pid
3. Create script file
touch ly.sh

Insert image description here

4. Add the following command to your shell file

Don't be lazy, just do it by hand, otherwise it may not work!

#! /bin/bash
kill -9 $(netstat -nlp | grep :81 | awk '{print $7}' | awk -F"/" '{ print $1 }')

Insert image description here

5. Execute the .sh file. Both bash and sh can be used. Just choose one.
bash 文件名   
sh 文件名

Insert image description here

6. Write jar restart script

After the above script is killed, you still need to manually restart it. It is better to write a script that can both kill and help start, in one step.

The contents of the file are as follows:

#! /bin/bash
#jar名称
JAR_PATH=jeecg-boot-module-system-3.0.jar
PID=$(ps -ef | grep $JAR_PATH | grep -v grep | awk '{ print $2 }')
if [ -z $PID ]
then
 echo Application is already stopped
else
 echo kill $PID
 kill -9 $PID
fi

java -jar $JAR_PATH &

Note: This script file is best located in the same directory as the jar.

Insert image description here

Okay, let’s try it out!

Notice! ! !
That #!/bin/bash is a must, otherwise an exception will occur, because /bin/bash represents the root directory

Guess you like

Origin blog.csdn.net/qq_45752401/article/details/110562964