2014-09-30 193 views
2

摘要: 流氓java进程来自延迟停止的服务,从而阻止服务返回。如何使用批处理脚本在WINDOWS中优先通过端口80终止CLOSE_WAIT状态进程

停止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相当于协助完成这项工作?

回答

3

在这里你有什么事情,你可以使用(运行它作为一个.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文件),但我认为这是更具可读性。

UPDATE

上面示出的脚本的改进,可以通过使用单引号和管道辛博尔之前插入记号(^)在包围所述管道命令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为脱字号解决方案

+0

好吧,我会给这个镜头。看起来很有希望。有没有办法做到这一点,没有输出文件偶然......? – 2014-09-30 02:40:04

+0

查看答案更新:) – Victor 2014-09-30 03:27:59

+0

作品。非常感谢。 – 2014-09-30 21:54:51