ps

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

README

ps

Go Reference GitHub Action Status

Package ps provides functionality to find, list and inspect operating system processes, without using cgo or external binaries.

Supported operating systems: Linux, macOS, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, Solaris/Illumos, Windows

Not all process information may be supported on all platforms. See the Go package reference for details.

This package is inspired by the github.com/mitchellh/go-ps and github.com/keybase/go-ps packages (the latter being a fork of the former). However, this package supports more operating systems, provides extended process information and uses only functionality from the Go standard libary and the golang.org/x/sys/unix and golang.org/x/sys/windows packages to retrieve information from the operating system, i.e. without using cgo or shelling out to external programs.

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

func FindProcess(pid int) (Process, error)

FindProcess returns the process identified by pid or an error if no process with that identifier is found.

func Processes

func Processes() ([]Process, error)

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:

Directories

Path Synopsis
cmd
ps

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL