process

package module
v0.0.0-...-bae18b2 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2022 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package process provides functionality to find, list and inspect operating system processes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Memory

type Memory struct {
	Size     int
	Resident int
	Shared   int
}

type Pid

type Pid int

func (Pid) Memory

func (p Pid) Memory() (error, Memory)

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 information

func Find

func Find(pid int) (Process, error)

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

func FindByName

func FindByName(name string) ([]Process, error)

FindByName returns all processes found that indentify by given name.

func List

func List() ([]Process, error)

List returns all currently running processes.

Example
package main

import (
	"fmt"
	"os"
	"sort"
	"strings"
	"text/tabwriter"

	"catinello.eu/info/process"
)

func main() {
	procs, err := process.List()
	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