From 0f77685b1e951091a736e1ac857bff244e16ebb6 Mon Sep 17 00:00:00 2001 From: "583641232@qq.com" <583641232@qq.com> Date: Sat, 6 Jul 2024 13:52:56 +0800 Subject: [PATCH] :sparkles: go --- main.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/main.js b/main.js index ecb0247..9ac2c55 100644 --- a/main.js +++ b/main.js @@ -1,6 +1,6 @@ // Modules to control application life and create native browser window const { app, BrowserWindow } = require('electron') -const { exec } = require('child_process'); +const { execSync } = require('child_process'); const path = require('node:path') function createWindow () { @@ -47,21 +47,25 @@ app.on('window-all-closed', function () { }) app.on('before-quit', () => { + // 获取端口 let port = 30523; - exec(`netstat -ano | findstr :${port}`, (error, stdout, stderr) => { - // 查找stdout中匹配端口的行,并提取PID - if (stdout) { - const lines = stdout.trim().split('\r\n'); - for (const line of lines) { - const pidMatch = line.match(/LISTENING\s+(\d+)/);; - if (pidMatch) { - let pid = pidMatch[1]; - require('child_process').execSync(`taskkill /PID ${pid} -t -f`); - return; - } + + // 执行杀死进程的操作 + try { + const command = `netstat -ano | findstr :${port}`; + const output = execSync(command).toString(); + const lines = output.trim().split('\n'); + for (const line of lines) { + const pidMatch = line.match(/LISTENING\s+(\d+)/); + if (pidMatch) { + const pid = pidMatch[1]; + execSync(`taskkill /PID ${pid} -t -f`); + break; } } - }); + } catch (error) { + console.error('无法杀死进程', error); + } });