Browse Source

go

main
583641232@qq.com 1 year ago
parent
commit
0f77685b1e
  1. 24
      main.js

24
main.js

@ -1,6 +1,6 @@
// Modules to control application life and create native browser window // Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow } = require('electron')
const { exec } = require('child_process');
const { execSync } = require('child_process');
const path = require('node:path') const path = require('node:path')
function createWindow () { function createWindow () {
@ -47,21 +47,25 @@ app.on('window-all-closed', function () {
}) })
app.on('before-quit', () => { app.on('before-quit', () => {
// 获取端口
let port = 30523; let port = 30523;
exec(`netstat -ano | findstr :${port}`, (error, stdout, stderr) => {
// 查找stdout中匹配端口的行,并提取PID
if (stdout) {
const lines = stdout.trim().split('\r\n');
// 执行杀死进程的操作
try {
const command = `netstat -ano | findstr :${port}`;
const output = execSync(command).toString();
const lines = output.trim().split('\n');
for (const line of lines) { for (const line of lines) {
const pidMatch = line.match(/LISTENING\s+(\d+)/);;
const pidMatch = line.match(/LISTENING\s+(\d+)/);
if (pidMatch) { if (pidMatch) {
let pid = pidMatch[1];
require('child_process').execSync(`taskkill /PID ${pid} -t -f`);
return;
const pid = pidMatch[1];
execSync(`taskkill /PID ${pid} -t -f`);
break;
} }
} }
} catch (error) {
console.error('无法杀死进程', error);
} }
});
}); });

Loading…
Cancel
Save