-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (56 loc) · 1.25 KB
/
main.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/v3/process"
)
var (
userModelId string
zombieOnly bool
)
func killElectron() error {
processList, err := process.Processes()
if err != nil {
return errors.Wrap(err, "Error getting process list")
}
for _, p := range processList {
name, err := p.Name()
if err != nil {
continue
}
cmdLine, err := p.Cmdline()
if err != nil {
continue
}
if (userModelId != "" && strings.Contains(cmdLine, fmt.Sprintf("--app-user-model-id=%s", userModelId))) || (userModelId == "" && name == "electron" || name == "electron.exe") {
pp, err := p.Parent()
if zombieOnly {
if err != nil {
_ = p.Terminate()
}
} else {
_ = p.Terminate()
if err == nil {
_ = pp.Terminate()
}
}
}
}
return nil
}
func main() {
flag.StringVar(&userModelId, "user-model-id", "", "electron app user model id")
flag.BoolVar(&zombieOnly, "zombie-only", false, "kill only zombie processes")
flag.Parse()
if userModelId == "" && !zombieOnly {
fmt.Fprintf(os.Stderr, "zombie-only is required when user-model-id is empty\n")
os.Exit(1)
}
if err := killElectron(); err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
}