Linux学习笔记(二):实战-根据微服务端口号关闭进程

前言

现在项目组基本都用Springboot,每个服务占用一个端口号,有时需要选择性的关闭,但在任务管理器上他们的名称都是java.exe,无法区分,这才学以致用。

killPort.sh

作用:根据端口号查询PID(process id),再根据PID杀死进程

#!/bin/bash

if [[ "$1" = "" ]]
then
	echo "port num must not be null"
	exit 2
fi
pidInfo=`netstat -ano |findstr ${1}`
if [[ -z $pidInfo ]]; then
echo "cannot find pid by port ${1}"
exit 0
fi
echo "pid info:"
echo "$pidInfo"
pid=${pidInfo##* }
taskkill -F -PID ${pid}

进阶

执行shell脚本总要指定好路径,这样很不方便,所以在环境变量path中把这两个命令的文件夹路径追加上去,那么在任意地方都可以直接调用了。
我是放在了F:shell_home,在path中追加此路径即可。

效果

原文地址:https://www.cnblogs.com/yw0219/p/9210732.html