40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
|
|
|
|
async function killport(port) {
|
|
$logger.info(`kill process on port ${port}`);
|
|
const util = require('util');
|
|
const exec = util.promisify(require('child_process').exec);
|
|
|
|
try {
|
|
// 查 PID
|
|
let { stdout : stdout1 } = await exec(`netstat -ano | findstr "LISTENING" | findstr /C:":${port} "`)
|
|
|
|
const output = stdout1.split('\n');
|
|
const outputLines = output[0].trim().split(' ')
|
|
const pid = outputLines[outputLines.length - 1];
|
|
$logger.debug(`绑定端口号 ${port} 的进程 PID=${pid}`)
|
|
|
|
|
|
// 查进程信息
|
|
let { stdout : stdout2 } = await exec(`tasklist /FI "PID eq ${pid}" /V /FO CSV`);
|
|
const tasklistOutput = stdout2.trim();
|
|
const processInfo = tasklistOutput.split('\n')[1].split('","');
|
|
const processName = processInfo[0].slice(1) ;
|
|
|
|
$logger.info(`绑定端口号=${port}; 进程 PID=${pid}; 程序=${processName};`);
|
|
|
|
// Kill 进程
|
|
await exec(`taskkill /PID ${pid} /F`)
|
|
|
|
} catch (e) {
|
|
if ( 1 === e.code ) {
|
|
$logger.error("此端口未被使用,或清理进程失败。")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
killport
|
|
} |