-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_test.go
More file actions
43 lines (37 loc) · 887 Bytes
/
example_test.go
File metadata and controls
43 lines (37 loc) · 887 Bytes
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
// Copyright 2021 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ps_test
import (
"cmp"
"fmt"
"os"
"slices"
"strings"
"text/tabwriter"
"github.com/tklauser/ps"
)
func ExampleProcesses() {
procs, err := ps.Processes()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to list processes: %v\n", err)
return
}
slices.SortFunc(procs, func(a, b ps.Process) int {
return cmp.Compare(a.PID(), b.PID())
})
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "PID\tPPID\tUID\tCOMMAND\n")
for _, p := range procs {
exeArgs := ""
if args := p.ExecutableArgs(); len(args) > 1 {
exeArgs = " " + strings.Join(args[1:], " ")
}
fmt.Fprintf(w, "%d\t%d\t%d\t%s%s\n",
p.PID(),
p.PPID(),
p.UID(),
p.ExecutablePath(), exeArgs)
}
w.Flush()
}