gpt4 book ai didi

windows - 如何使用批处理脚本最好通过 WINDOWS 中的端口 80 终止 CLOSE_WAIT 状态进程

转载 作者:可可西里 更新时间:2023-11-01 10:05:51 29 4
gpt4 key购买 nike

总结:流氓 java 进程在停止的服务中徘徊,阻止服务返回。

停止java服务有时不会杀死java进程,它会无休止地将其锁定在CLOSE_WAIT状态,因此当服务尝试返回时端口80仍在IP上使用,因此服务无法启动

执行 netstat -ano 返回 IP/PORT 组合的 PID,然后我可以手动终止它。我想避免自己不得不这样做。我想在我们的服务重启脚本中添加一个部分,该部分将终止任何处于 CLOSE_WAIT 状态的端口 80 进程。

我可以在 Linux 中很容易地做到这一点:

$ netstat -anp |\
grep ':80 ' |\
grep CLOSE_WAIT |\
awk '{print $7}' |\
cut -d \/ -f1 |\
grep -oE "[[:digit:]]{1,}" |\
xargs kill

但我的 Windows 批处理脚本技能很差。

任何人都可以在 Windows 中协助完成这项工作吗?

最佳答案

这里有一些你可以使用的东西(作为 .bat 运行):

echo off
netstat -ano | find "127.0.0.1:80" | find "CLOSE_WAIT" > out.txt

FOR /F "tokens=5 delims= " %%I IN (out.txt) DO (
TASKKILL %%I
)

del out.txt

它可以作为单个命令完成(没有 .bat 文件),但我认为这更具可读性。

更新

上面显示的脚本的改进,您可以通过在 for 循环中使用单引号和管道符号 (|) 之前的脱字符 (^) 将管道命令括起来来避免使用临时文件:

FOR /F "tokens=5 delims= " %%I IN (
'netstat -ano ^| find "127.0.0.1:80" ^| find "CLOSE_WAIT"'
) DO (
taskkill /PID %%I
)

for/F 命令的帮助解释得更好:

Finally, you can use the FOR /F command to parse the output of a command. You do this by making the file-set between the parenthesis a back quoted string. It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file. So the following example: FOR /F "usebackq delims==" %i IN (set) DO @echo %i

归功于 this answer对于插入符解决方案

关于windows - 如何使用批处理脚本最好通过 WINDOWS 中的端口 80 终止 CLOSE_WAIT 状态进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26110843/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com