From 589b933f7ce3244ecf8f8b9b4b3fa8bfb217beb9 Mon Sep 17 00:00:00 2001 From: aaron <462826@qq.com> Date: Fri, 4 Mar 2022 14:04:32 +0800 Subject: [PATCH] kill ztf process by electron --- client/src/service.js | 45 ++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/client/src/service.js b/client/src/service.js index 523fb57e..f542a8bc 100644 --- a/client/src/service.js +++ b/client/src/service.js @@ -1,5 +1,5 @@ import path from 'path'; -import cp, {spawn} from 'child_process'; +import cp, {exec, spawn} from 'child_process'; import os from 'os'; import {app} from 'electron'; import express from 'express'; @@ -269,29 +269,38 @@ export function killZtfServer() { logInfo(`>> is windows`); const cmd = 'WMIC path win32_process where "Commandline like \'%%' + uuid + '%%\'" get Processid,Caption'; - logInfo(`list process cmd : ${cmd}`); + let msg = `list process cmd : ${cmd}` + console.log(msg); + logInfo(msg); const cp = require('child_process'); - cp.exec(cmd, function (error, stdout, stderr) { - // console.log('stdout: ' + stdout + '; stderr: ' + stderr + '; error: ' + error + '.'); - const lines = stdout.split('\n') - lines.forEach(function(line){ - line = line.trim() - console.log('<' + line + '>') - const columns = line.split(/\s/) + const stdout = cp.execSync(cmd).toString().trim() + msg = `exec ${cmd}, stdout: ${stdout}` + console.log(msg); + logInfo(msg) - if (columns.length > 2) { - const pid = columns[2].trim() - console.log(`pid=${pid}`); + const lines = stdout.split('\n') + lines.forEach(function(line){ + line = line.trim() + console.log(`<${line}>`) + logInfo(`<${line}>`) + const columns = line.split(/\s/) - const cpKill = require('child_process'); + if (columns.length > 2) { + const pid = columns[2].trim() + console.log(`pid=${pid}`); + logInfo(`pid=${pid}`) + + if (pid && parseInt(pid, 10) === pid) { const killCmd = `taskkill /F /pid ${pid}` - logInfo(`list cmd : ${killCmd}`); - cpKill.exec(killCmd, function (error, stdout, stderr) { - console.log('stdout: ' + stdout + '; stderr: ' + stderr + '; error: ' + error + '.'); - }); + + const cp = require('child_process'); + const stdout = cp.execSync(killCmd).toString().trim() + msg = `exec ${cmd}, stdout: ${stdout}` + console.log(msg); + logInfo(msg) } - }); + } }); } -- GitLab