標籤:windows服務 資訊 指令碼 taskkill 查詢 func return exe line
1.根據連接埠號碼,查詢進程資訊命令:
netstat -aon | findstr "連接埠號碼"
2.根據pid殺死進程命令:
taskkill /F /pid 進程號
完整代碼:
const windowsServiceStop = function(port){ let netstat_command = ‘netstat -aon | findstr \":‘+port+‘\"‘; console.log(‘查詢服務進程...‘+netstat_command); exec(netstat_command,{maxBuffer: 5000 * 1024},(err, stdout, stderr) => { if (err) { console.log(‘查詢服務進程異常:‘+err); return false; } let line = stdout.split(‘\n‘)[0]; //第一行資訊 console.log(‘查詢成功,進程資訊:‘+line); let p=line.trim().split(/\s+/); let pid = p[4]; if(!pid || pid.length == 0){ console.log(‘擷取進程id失敗‘); return false; } let taskkill_command = ‘taskkill /F /pid ‘+pid; console.log(‘關閉服務...‘+taskkill_command); exec(taskkill_command,{maxBuffer: 5000 * 1024},function(err, stdout, stderr){ if(err){ console.log(‘關閉服務異常:‘+err); return false; } console.log(‘服務關閉成功‘); return true; }); }); };
參考:
Node.js指令碼殺掉佔用連接埠的進程
Nodejs關閉windows服務進程