-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
123 lines (107 loc) · 2.91 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/spf13/cobra"
)
func main() {
var processName string
var dryRun bool
var rootCmd = &cobra.Command{
Use: "kp",
Short: "A CLI to kill processes by regex",
Run: func(cmd *cobra.Command, args []string) {
if processName == "" {
fmt.Println("Please provide a process name regex using --process-name or -p")
return
}
processes, err := listProcesses()
if err != nil {
fmt.Printf("Error listing processes: %v\n", err)
return
}
matchedProcesses := filterProcesses(processes, processName)
if dryRun {
fmt.Println("Dry run mode. The following processes would be killed:")
for _, p := range matchedProcesses {
fmt.Printf("PID: %s, Name: %s\n", p.PID, p.Name)
}
} else {
if len(matchedProcesses) > 0 {
fmt.Println("The following processes match the regex:")
for _, p := range matchedProcesses {
fmt.Printf("PID: %s, Name: %s\n", p.PID, p.Name)
}
fmt.Print("Do you want to kill these processes? (y/n): ")
reader := bufio.NewReader(os.Stdin)
response, _ := reader.ReadString('\n')
response = strings.TrimSpace(response)
if strings.ToLower(response) == "y" {
for _, p := range matchedProcesses {
fmt.Printf("Killing process PID: %s, Name: %s\n", p.PID, p.Name)
err := killProcess(p.PID)
if err != nil {
fmt.Printf("Failed to kill process PID: %s, error: %v\n", p.PID, err)
}
}
} else {
fmt.Println("No processes were killed.")
}
} else {
fmt.Println("No matching processes found.")
}
}
},
}
rootCmd.Flags().StringVarP(&processName, "process-name", "p", "", "Regex to match process names")
rootCmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "Dry run mode, do not kill processes")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type Process struct {
PID string
Name string
}
func listProcesses() ([]Process, error) {
out, err := exec.Command("ps", "-e", "-o", "pid,comm").Output()
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
var processes []Process
for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) >= 2 {
processes = append(processes, Process{PID: fields[0], Name: fields[1]})
}
}
return processes, nil
}
func filterProcesses(processes []Process, regex string) []Process {
var matchedProcesses []Process
r, err := regexp.Compile(regex)
if err != nil {
fmt.Printf("Invalid regex: %v\n", err)
return matchedProcesses
}
for _, p := range processes {
if r.MatchString(p.Name) {
matchedProcesses = append(matchedProcesses, p)
}
}
return matchedProcesses
}
func killProcess(pid string) error {
cmd := exec.Command("kill", pid)
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to kill process PID: %s, error: %w", pid, err)
}
return nil
}