bat查看java进程 过滤_通过查找.BAT中使用的端口来终止进程
回答(14)
2 years ago
這是一個讓你入門的命令:
FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO @ECHO TaskKill.exe /PID %%P
如果您對批處理文件有信心,請刪除 @ECHO .
FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :8080') DO TaskKill.exe /PID %%P
請注意,對于不同的操作系統,您可能需要稍微更改一下 . 例如,在Windows 7上,您可能需要 tokens=5 而不是 tokens=4 .
How this works
FOR /F ... %variable IN ('command') DO otherCommand %variable...
這使您可以執行 command ,并循環其輸出 . 每一行都將填入 %variable ,并且可以隨時隨地在 otherCommand 中展開,無論你喜歡什么 . 實際使用中的 %variable 只能有一個單字母的名稱,例如 %V .
"tokens=4 delims= "
這允許你按空格分割每一行,并取出該行中的第4個塊,并將其填入 %variable (在我們的例子中, %%P ) . delims 看起來是空的,但這個額外的空間實際上很重要 .
netstat -a -n -o
只需運行它就可以找到答案 . 根據命令行幫助,它“顯示所有連接和偵聽端口 . ”,“以數字形式顯示地址和端口號 . ”和“顯示與每個連接關聯的擁有進程ID” . 我只是使用這些選項,因為別人建議它,它碰巧工作:)
^|
這將獲取第一個命令或程序的輸出( netstat )并將其傳遞給第二個命令程序( findstr ) . 如果直接在命令行中使用它,而不是在命令字符串中,則使用 | 而不是 ^| .
findstr :8080
這會過濾傳遞給它的任何輸出,只返回包含 :8080 的行 .
TaskKill.exe /PID
這會使用進程ID終止正在運行的任務 .
%%P instead of %P
這在批處理文件中是必需的 . 如果在命令提示符下執行此操作,則應使用 %P .
2 years ago
打開命令提示符并運行以下命令
C:\Users\username>netstat -o -n -a | findstr 0.0:3000
TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING 3116
C:\Users\username>taskkill /F /PID 3116
,這里3116是進程ID
2 years ago
要在命令行上查找特定進程,請使用以下命令:8080是進程使用的端口
netstat -ano | findstr 8080
殺死進程使用下面命令21424是進程ID
taskkill /pid 21424 /F
2 years ago
使用Merlyn的解決方案導致其他應用程序像firefox一樣被殺死 . 這些進程使用相同的端口,但不是作為監聽器:
例如:
netstat -a -n -o | findstr :8085
TCP 0.0.0.0:8085 0.0.0.0:0 LISTENING 6568
TCP 127.0.0.1:49616 127.0.0.1:8085 TIME_WAIT 0
TCP 127.0.0.1:49618 127.0.0.1:8085 TIME_WAIT 0
因此,可以通過向findstr添加“LISTENING”來排除這些,如下所示:
FOR /F "tokens=5 delims= " %%P IN ('netstat -a -n -o ^| findstr :8085.*LISTENING') DO TaskKill.exe /PID %%P
2 years ago
要列出在端口8080上運行的所有進程,請執行以下操作 .
netstat -ano |找到“8080”
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 10612
TCP [::]:8080 [::]:0 LISTENING 10612
然后殺死進程運行以下命令
taskkill / F / PID 10612
2 years ago
謝謝大家,只是添加一些進程不會關閉,除非/ F強制開關也與TaskKill一起發送 . 同時使用/ T開關,將關閉進程的所有輔助線程 .
C:> FOR / F“tokens = 5 delims =”%P IN('netstat -a -n -o ^ |
findstr:2002')DO TaskKill.exe / PID%P / T / F.
對于服務,有必要獲取服務的名稱并執行:
sc停止ServiceName
2 years ago
剛完成:
我想殺死連接到特定端口但不是進程偵聽的所有進程
端口9001的命令(在cmd shell中)是:
FOR /F "tokens=5 delims= " %P IN ('netstat -ano ^| findstr -rc:":9001[ ]*ESTA"') DO TaskKill /F /PID %P
findstr :
r用于表達式,c用于精確鏈匹配 .
[] *用于匹配空格
netstat :
a - >全部
n - >不解決(更快)
o - > pid
它的工作原理是因為netstat打印出源端口然后打印出目標端口然后打開ESTABLISHED
2 years ago
如果要殺死正在偵聽端口8080的進程,可以使用PowerShell . 只需將Get-NetTCPConnection cmdlet與Stop-Process結合使用即可 .
經過測試,應該可以在Windows 10或Windows Server 2016上使用PowerShell 5.但是,我想它應該也適用于安裝了PowerShell 5的舊版Windows .
這是一個例子:
PS C:\> Stop-Process -Id (Get-NetTCPConnection -LocalPort 8080).OwningProcess
Confirm
Are you sure you want to perform the Stop-Process operation on the following item: MyTestServer(9408)?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
2 years ago
與Merlyn的回應類似,但這個也處理了這些情況:
端口號實際上是您不想要的另一個較長端口號的左子串 . 你想搜索一個 exact 端口號,這樣你就不會殺死一個隨機的,無辜的進程!
腳本代碼需要能夠運行多次并且每次都是正確的,而不是顯示較舊的錯誤答案 .
這里是:
set serverPid=
for /F "tokens=5 delims= " %%P in ('netstat -a -n -o ^| findstr /E :8080 ') do set serverPid=%%P
if not "%serverPid%" == "" (
taskkill /PID %serverPid%
) else (
rem echo Server is not running.
)
2 years ago
腳步:
轉到 apache tomcat 服務器的 conf 文件夾 . 在我的case,它 apache-tomcat-7.0.61\conf 因為我正在使用apache-tomcat-7.0.61
打開 server.xml 并根據需要將端口號從8080更改為任何其他端口 . 例如:8081,8082,8087等
現在轉到 bin 文件夾并運行 shutdown.bat
現在通過eclipse重啟服務器 .
現在您的項目可以正常運行而不會中斷 .
2 years ago
如果有人在尋找Powershell腳本:
function Search-And-Destroy
{
param ( [Parameter(Mandatory=$true)][string]$port )
$lines = netstat -a -o -n | findstr $port
$ports = @()
ForEach($line In $lines)
{
$res = $($lines -split '\s+')
$ports += $res[5]
}
$ports = $ports | select -uniq
ForEach($port In $ports)
{
echo $(taskkill /F /PID $port)
}
}
此函數基本上執行上述函數的功能,但它采用Powershell腳本格式,因此您可以將其添加到Powershell配置文件中 . 要查找您的 Profiles 的位置,請轉到PowerShell并輸入 echo $profile
2 years ago
將其粘貼到命令行中
FOR /F "tokens=5 delims= " %P IN ('netstat -ano ^| find "LISTENING" ^| find ":8080 "') DO (TASKKILL /PID %P)
如果你想在批量pu %%P 而不是 %P 中使用它
2 years ago
如果你是系統你不能結束它的任務 . 試試這個命令
x:> net stop http / y
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的bat查看java进程 过滤_通过查找.BAT中使用的端口来终止进程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: eclipse java jdk_设置E
- 下一篇: java timestamp时间差_关于