Documentation ¶
Overview ¶
Package ps provides functionality to find, list and inspect operating system processes, without using cgo or external binaries.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Process ¶
type Process interface { // PID returns the process ID for this process. PID() int // PPID returns the parent process ID for this process. PPID() int // UID returns the numeric user ID for this process. On Windows, it // always returns -1. UID() int // GID returns the numeric group ID for this process. On Windows, it // always returns -1. GID() int // ExecutablePath returns the full path to the executable of this // process. This information might not be available on all platforms or // if the executable was removed while the process was still running. ExecutablePath() string // ExecutableArgs returns the command line arguments for this process, // including the executable name. This information might not be // available on all platforms. ExecutableArgs() []string // Command returns the command or executable name running this process. // On some platforms (e.g. macOS and the BSDs) this name might be // truncated. Command() string // CreationTime returns the creation time for this process. CreationTime() time.Time }
Process is the generic interface for common process information.
func FindProcess ¶
FindProcess returns the process identified by pid or an error if no process with that identifier is found.
func Processes ¶
Processes returns all currently running processes.
Example ¶
package main import ( "fmt" "os" "sort" "strings" "text/tabwriter" "github.com/tklauser/ps" ) func main() { procs, err := ps.Processes() if err != nil { fmt.Fprintf(os.Stderr, "failed to list processes: %v\n", err) return } sort.Slice(procs, func(i, j int) bool { return procs[i].PID() < procs[j].PID() }) w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0) fmt.Fprintf(w, "PID\tPPID\tUID\tCOMMAND") 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", p.PID(), p.PPID(), p.UID(), p.ExecutablePath(), exeArgs) } w.Flush() }
Output:
Source Files ¶
Click to show internal directories.
Click to hide internal directories.