更改关闭进程的方法

This commit is contained in:
unknown 2020-04-20 20:39:05 +08:00
parent 15ae052ccd
commit e393419d5e
5 changed files with 25 additions and 98 deletions

View File

@ -32,30 +32,10 @@ CacheIcons = async tasks => {
} }
} }
kill = (taskname, taskpath) => { kill = async (pid, restart) => {
window.taskkill(taskname, taskpath, err => { await window.taskkill(pid, restart)
if (err) { window.tasks = await tasklist();
$("#infopannel").css({ show(window.text);
"background": "#EF5350"
});
$("#infopannel").html(err).fadeIn(300).delay(3000).fadeOut(300);
} else {
if (taskpath == undefined) {
$('.select').remove();
let tasknum = $(".taskinfo").length
utools.setExpendHeight(tasknum > 11 ? 550 : 50 * tasknum);
} else {
$("#infopannel").css({
"background": "#83bf40"
});
$("#infopannel").html('重启进程成功!').fadeIn(300).delay(3000).fadeOut(300);
}
window.tasklist((task) => {
window.tasks = task
show(window.text);
});
}
});
} }
search = (t, text) => { search = (t, text) => {
@ -143,7 +123,7 @@ utools.onPluginEnter( async ({ code, type, payload }) => {
$("#tasklist").on('mousedown', '.taskinfo', function (e) { $("#tasklist").on('mousedown', '.taskinfo', function (e) {
if (1 == e.which) { if (1 == e.which) {
kill($(this).attr('id')); kill($(this).attr('id'), false);
} else if (3 == e.which) { } else if (3 == e.which) {
kill($(this).attr('id'), $(this).children(".path").html().replace(/\\/g, '/')) kill($(this).attr('id'), $(this).children(".path").html().replace(/\\/g, '/'))
} }
@ -163,7 +143,7 @@ $(document).keydown(e => {
if (event.shiftKey) { if (event.shiftKey) {
kill($(".select").attr('id'), $(".select").children(".path").html().replace(/\\/g, '/')) kill($(".select").attr('id'), $(".select").children(".path").html().replace(/\\/g, '/'))
} else { } else {
kill($(".select").attr('id')); kill($(".select").attr('id'), false);
} }
break; break;
case 38: case 38:

View File

@ -1,9 +1,10 @@
const os = require('os') const os = require('os')
const iconv = require('iconv-lite') const iconv = require('iconv-lite')
const { spawn, exec, execFile, execSync } = require("child_process") const { exec, execFile, execSync } = require("child_process")
const path = require("path") const path = require("path")
const fs = require('fs'); const fs = require('fs');
const jschardet = require('jschardet'); const process = require('process');
// const jschardet = require('jschardet');
isDev = /unsafe-\w+\.asar/.test(__dirname) ? false : true isDev = /unsafe-\w+\.asar/.test(__dirname) ? false : true
@ -17,31 +18,10 @@ GetBinPath = ExeFile => {
} }
} }
isWin = os.platform() == 'win32' ? true : false; isWin = os.platform() == 'win32' ? true : false;
totalMem = os.totalmem(); totalMem = os.totalmem();
powershell = (cmd, callback) => {
const ps = spawn('powershell', ['-NoProfile', '-Command', cmd], { encoding: 'buffer' })
let chunks = [], err_chunks = [], size = 0, err_size = 0;
ps.stdout.on('data', chunk => {
chunks.push(chunk);
size += chunk.length;
})
ps.stderr.on('data', err_chunk => {
err_chunks.push(err_chunk);
err_size += err_chunk.length;
})
ps.on('close', code => {
let stdout = Buffer.concat(chunks, size);
stdout = stdout.length ? iconv.decode(stdout, jschardet.detect(stdout).encoding) : '';
let stderr = Buffer.concat(err_chunks, err_size);
stderr = stderr.length ? iconv.decode(stderr, jschardet.detect(stderr).encoding) : '';
callback(stdout, stderr)
})
}
tasklist = () => tasklist = () =>
new Promise((reslove, reject) => { new Promise((reslove, reject) => {
{ {
@ -55,30 +35,13 @@ tasklist = () =>
}); });
reslove(data); reslove(data);
}) })
// exec('net session > NULL && echo 1 || echo 0', (err, stdout, stderr) => {
// let isAdmin = parseInt(stdout),
// IncludeUserName = isAdmin ? '-IncludeUserName' : '',
// UserName = isAdmin ? ',UserName' : '';
// powershell(`Get-Process ${IncludeUserName} | sort-object ws -descending | Select-Object ProcessName,Path,Description,WorkingSet${UserName} | ConvertTo-Json`, (stdout, stderr) => {
// stderr && console.log(stderr);
// tasklist = JSON.parse(stdout);
// reslove(tasklist);
// });
// })
} else { } else {
exec('ps -A -o pid -o %cpu -o %mem -o user -o comm | sed 1d | sort -rnk 3', (err, stdout, stderr) => { exec('ps -A -o pid -o %cpu -o %mem -o user -o comm | sed 1d | sort -rnk 3', (err, stdout, stderr) => {
lines = stdout.split('\n'); lines = stdout.split('\n');
lines.forEach(line => { lines.forEach(line => {
if (line) { if (line) {
l = /(\d+)\s+(\d+[\.|\,]\d+)\s+(\d+[\.|\,]\d+)\s+(.*?)\s+(.*)/.exec(line); l = /(\d+)\s+(\d+[\.|\,]\d+)\s+(\d+[\.|\,]\d+)\s+(.*?)\s+(.*)/.exec(line);
dict = { dict = { pid: l[1], cpu: l[2], mem: l[3], usr: l[4], path: l[5], nam: l[5].split('/').pop(), }
pid: l[1],
cpu: l[2],
mem: l[3],
usr: l[4],
path: l[5],
nam: l[5].split('/').pop(),
}
let reg = /.*?\/Applications\/.*?\.app\//.exec(dict.path) let reg = /.*?\/Applications\/.*?\.app\//.exec(dict.path)
dict.app = reg ? reg[0] : false; dict.app = reg ? reg[0] : false;
tasklist.push(dict); tasklist.push(dict);
@ -90,20 +53,20 @@ tasklist = () =>
} }
}) })
taskkill = (pid, restart) =>
taskkill = (task, path, callback) => { new Promise((reslove, reject) => {
if (isWin) { try {
let restart = path == undefined ? '' : `;Start-Process -FilePath "${path}"`; process.kill(pid);
powershell(`Stop-Process -Name ${task}${restart}`, (stdout, stderr) => { } catch (error) {
callback(stderr.split('\n')[0]) utools.showNotification(error);
}); reject(error);
} else { }
let restart = path == undefined ? '' : `&& "${path}"`; if(restart){
exec(`kill -9 ${task}${restart}`, (err, stdout, stderr) => { utools.showNotification('重启进程成功!')
callback(stderr); exec(restart);
}); }
} reslove(true);
} })
findThirdIndex = (str, cha) => { findThirdIndex = (str, cha) => {
var x = str.indexOf(cha); var x = str.indexOf(cha);
@ -125,7 +88,6 @@ icns2Base64 = icns => {
return b64 return b64
} }
GetIcons = PathList => GetIcons = PathList =>
new Promise((reslove, reject) => { new Promise((reslove, reject) => {
if (isWin) { if (isWin) {
@ -153,18 +115,3 @@ GetIcons = PathList =>
reslove(data) reslove(data)
} }
}) })
// var initTime = new Date().getTime();
// powershell(`Get-Process | sort-object ws -descending | Select-Object ProcessName,Path,Description,WorkingSet | ConvertTo-Json`, (stdout, stderr) => {
// stderr && console.log(stderr);
// tasklist = JSON.parse(stdout);
// console.log(tasklist);
// var EndTime = new Date().getTime();
// console.log(EndTime - initTime);
// })
// execFile(path.join('bin', 'ProcessKiller.exe'), ['getProcess'], (err, stdout, stderr) => {
// console.log(JSON.parse(stdout));
// var EndTime = new Date().getTime();
// console.log(EndTime - initTime);
// })

Binary file not shown.

Binary file not shown.

Binary file not shown.