diff --git a/restartable.go b/restartable.go index 659a3eb..1735317 100644 --- a/restartable.go +++ b/restartable.go @@ -316,14 +316,10 @@ func runProcessMonitor(lister ProcessLister, opts Opts, openProc func(int) (Proc fmt.Printf("%s\t%s\t%s\t%-20s\t%20s\t%s\n", "PID", "PPID", "UID", "User", "Service", "Command") } - channel := make(map[int]chan *ProcessInfo, len(pids)) - for _, pid := range pids { - channel[pid] = make(chan *ProcessInfo, 1) - } - - for _, pid := range pids { - go func(pid int) { - defer close(channel[pid]) + channel := make(chan *ProcessInfo, len(pids)) + go func() { + defer close(channel) + for _, pid := range pids { fs, err := openProc(pid) if err != nil { if !errors.Is(err, unix.ENOENT) { @@ -335,17 +331,14 @@ func runProcessMonitor(lister ProcessLister, opts Opts, openProc func(int) (Proc info, err := getProcessInfo(fs, opts.verbose, opts.user) if err != nil { log.Print(err) + } else if info != nil { + channel <- info } - channel[pid] <- info - }(pid) - } + } + }() services := make(map[string]bool) - for _, pid := range pids { - proc := <-channel[pid] - if proc == nil { - continue - } + for proc := range channel { if opts.short < 3 { fmt.Printf("%d\t%d\t%d\t%-20s\t%20s\t%s\n", proc.Pid, proc.Ppid, proc.Uid, getUser(proc.Uid), proc.Service, proc.Command) } else if proc.Service != "-" {