MCPcopy
hub / github.com/volcengine/MineContext / killProcessByPortSync

Function killProcessByPortSync

frontend/src/main/backend.ts:662–709  ·  view source on GitHub ↗
(port: number)

Source from the content-addressed store, hash-verified

660
661// 同步方式通过端口杀死进程
662function killProcessByPortSync(port: number) {
663 const { execSync } = require('child_process')
664
665 try {
666 if (process.platform === 'win32') {
667 // Windows
668 const output = execSync(`netstat -ano | findstr :${port}`, { encoding: 'utf8', timeout: 3000 })
669 const lines = output.split('\n')
670 const pids = new Set()
671
672 lines.forEach((line) => {
673 const match = line.match(/\s+(\d+)\s*$/)
674 if (match && match[1] !== '0') {
675 pids.add(match[1])
676 }
677 })
678
679 pids.forEach((pid) => {
680 try {
681 logToBackendFile(`Sync killing process on port ${port}, PID: ${pid}`)
682 execSync(`taskkill /PID ${pid} /F`, { timeout: 3000 })
683 logToBackendFile(`Successfully killed PID ${pid}`)
684 } catch (e) {
685 logToBackendFile(`Failed to kill PID ${pid}: ${e}`)
686 }
687 })
688 } else {
689 // Unix/Linux/macOS
690 const output = execSync(`lsof -ti:${port}`, { encoding: 'utf8', timeout: 3000 })
691 const pids = output
692 .trim()
693 .split('\n')
694 .filter((pid) => pid)
695
696 pids.forEach((pid) => {
697 try {
698 logToBackendFile(`Sync killing process on port ${port}, PID: ${pid}`)
699 process.kill(parseInt(pid), 'SIGKILL')
700 logToBackendFile(`Successfully killed PID ${pid}`)
701 } catch (e) {
702 logToBackendFile(`Failed to kill PID ${pid}: ${e}`)
703 }
704 })
705 }
706 } catch (error: any) {
707 logToBackendFile(`No process found on port ${port} or error: ${error.message}`)
708 }
709}
710
711// 获取当前后端端口号
712export function getBackendPort(): number {

Callers 1

stopBackendServerSyncFunction · 0.85

Calls 2

logToBackendFileFunction · 0.85
addMethod · 0.45

Tested by

no test coverage detected