如何在WINDOWS中使用批处理脚本,最好是通过端口80来杀死一个关闭状态进程?

时间:2022-04-30 16:24:30

Summary: Rogue java processes from lingering stopped services preventing service from returning.

总结:流氓java进程从延迟停止服务阻止服务返回。

Stopping a java service doesn't kill the java process on occasion, it locks it in a CLOSE_WAIT status endlessly, so port 80 is still being used on the IP when the service attempts to return, thus the service fails to start

停止java服务不会在某些情况下杀死java进程,它会在一个关闭的等待状态中不断地锁定它,因此当服务尝试返回时,仍然会在IP上使用端口80,因此服务无法启动。

Doing a netstat -ano returns the PID of the IP/PORT combination, then I can kill it manually. I'd like to prevent myself from having to do this. I'd like to add to our service restart script a section that will kill any port 80 process in the CLOSE_WAIT status.

使用netstat -ano返回IP/端口组合的PID,然后我可以手动杀死它。我想阻止自己做这件事。我想在我们的服务重启脚本中添加一个区段,它将在CLOSE_WAIT状态中杀死任何端口80进程。

I can do this in Linux easily enough:

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

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

But my windows batch scripting skills are pretty sub-par.

但是我的windows批处理脚本技术相当不正常。

Can anyone assist in a windows equivalent to get this done?

有谁能在windows上协助完成这项工作?

1 个解决方案

#1


3  

Here you have something you could use (run it as a .bat):

这里有一些你可以使用的东西(如: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

It can be done as a single command (without having the .bat file) but I think this is more readable.

它可以作为一个单独的命令来完成(没有.bat文件),但是我认为这更容易读懂。

UPDATE

更新

An improvement of the script shown above, you can avoid using the temporary file by enclosing the piped command in the for loop using single quotes and carets (^) before the pipes simbol (|):

改善上面所示的脚本,您可以避免使用临时文件通过封闭管道命令在for循环中使用单引号和克拉(^)在管道辛博尔(|):

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

The help of the for /F commands explains it better:

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

最后,您可以使用FOR /F命令来解析命令的输出。这样做的方法是在括号后面加上一个引用的字符串。它将被当作一个命令行来处理,它被传递给一个儿童CMD。EXE和输出被捕获到内存中,并将其解析为文件。下面的例子:FOR /F“usebackq delims==”%i IN (set) DO @echo %i。

Credits to this answer for the caret solution

这个问题的解决方案的学分。

#1


3  

Here you have something you could use (run it as a .bat):

这里有一些你可以使用的东西(如: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

It can be done as a single command (without having the .bat file) but I think this is more readable.

它可以作为一个单独的命令来完成(没有.bat文件),但是我认为这更容易读懂。

UPDATE

更新

An improvement of the script shown above, you can avoid using the temporary file by enclosing the piped command in the for loop using single quotes and carets (^) before the pipes simbol (|):

改善上面所示的脚本,您可以避免使用临时文件通过封闭管道命令在for循环中使用单引号和克拉(^)在管道辛博尔(|):

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

The help of the for /F commands explains it better:

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

最后,您可以使用FOR /F命令来解析命令的输出。这样做的方法是在括号后面加上一个引用的字符串。它将被当作一个命令行来处理,它被传递给一个儿童CMD。EXE和输出被捕获到内存中,并将其解析为文件。下面的例子:FOR /F“usebackq delims==”%i IN (set) DO @echo %i。

Credits to this answer for the caret solution

这个问题的解决方案的学分。