Skip to content

Commit

Permalink
add pid to kill command
Browse files Browse the repository at this point in the history
  • Loading branch information
haibeey committed Jul 19, 2024
1 parent 6766edb commit 2d544c7
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"

"github.com/olekukonko/tablewriter"
Expand All @@ -20,10 +21,10 @@ import (

var (
program = flag.String("program", "", "Program to start in host OS binary format for instance python")
programArgs = flag.String("args", "", "Space separated arguement to be pass to the program for instance python filename.py")
programArgs = flag.String("args", "", "Space separated argument to be pass to the program for instance python filename.py")
name = flag.String("name", "", "Name of the program")
show = flag.Bool("show", false, "Show list of process")
kill = flag.Bool("kill", false, "Kill process by name")
show = flag.Bool("show", false, "Show the list of all processes")
kill = flag.Bool("kill", false, "Kill process by name or pid")
)

type Entry struct {
Expand All @@ -47,8 +48,7 @@ func main() {

if *kill {
if len(*name) <= 0 {
fmt.Println(len(*name), *name)
log.Fatal("name must be passed")
log.Fatal("name or pid must be passed")
}

err := killEntry(*name)
Expand Down Expand Up @@ -254,7 +254,12 @@ func showEntry() {
data = append(data, []string{fmt.Sprintf("%d", entry.Pid), entry.Name})
proc, err := process.NewProcess(int32(entry.Pid))
if err != nil {
log.Fatalf("Error getting process: %s", err.Error())
if errors.Is(err, process.ErrorProcessNotRunning) {
killEntry(entry.Name)
continue
}

log.Fatalf("Error getting process with name(%s) and PID(%d). Error encountered %s", entry.Name, entry.Pid, err.Error())
}

cpuPercent, err := proc.CPUPercent()
Expand Down Expand Up @@ -298,7 +303,7 @@ func killEntry(name string) error {

el := EntryList{}
for _, entry := range entryList {
if entry.Name == name {
if entry.Name == name || strconv.Itoa(entry.Pid) == name {
proc, err := os.FindProcess(entry.Pid)
if err != nil {
log.Println(err)
Expand Down

0 comments on commit 2d544c7

Please sign in to comment.