-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkill.js
47 lines (38 loc) · 1.07 KB
/
pkill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const { execSync } = require('child_process')
async function pkill({ exe }) {
let pCommand = ''
if (process.platform === 'win32') {
pCommand = 'tasklist /FO csv /NH'
} else if (process.platform === 'linux') {
pCommand = 'ps -e -o "%c,%p"'
}
const pList = execSync(pCommand)
.toString()
.trim().split('\n')
.map((p) => {
const pS = p.split(',')
return { name: pS[0].replace(/((^")|("$))/g, '').trim(), pid: parseInt(pS[1].replace(/((^")|("$))/g, '').trim()) }
})
// ignore current run process
const pid = pList.filter((p) => p.name.includes(exe) && p.pid !== process.pid)
if (pid.length < 1) return
pid.forEach((p) => {
let kCommand = ''
if (process.platform === 'win32') {
kCommand = `taskkill /F /PID ${p.pid}`
} else if (process.platform === 'linux') {
kCommand = `kill -s 9 ${p.pid}`
}
try {
execSync(kCommand, { stdio: 'ignore' })
} catch (err) {
// pass
}
})
await new Promise((resolve) => {
setTimeout(() => { resolve(true) }, 100)
})
}
module.exports = {
pkill,
}