ps

package module
v0.0.0-...-1dca9ca Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2019 License: MIT Imports: 7 Imported by: 0

README

PS

A Go library which runs the Unix 'ps' command and returns structured information about the process.

This library essentially runs:

bash -c ps -aux | grep [p]rocess_name

And parses the output of the command.

Installation

$ go get github.com/umahmood/ps

Usage

package main

import (
    "fmt"

    "github.com/umahmood/ps"
)

func main() {
    proc, err := ps.Snapshot("firefox")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(proc.PID)
    fmt.Println("")
    fmt.Println(proc)
}

Output:

19352

User   : usman
PID    : 19352
CPU    : 0.00
MEM    : 8.10
VSZ    : 8353516
RSS    : 1350828
TTY    : ??
STAT   : S
START  : 22Dec18
TIME   : 334:44.58
COMMAND: /Applications/Firefox.app/Contents/MacOS/firefox

What do the output fields of the ps command mean?

Limitations

The library only matches unique running processes. If there are multiple processes with the same name, then an error is returned. i.e. if we have the following running processes:

- proc_foo --arg=1
- proc_foo --arg=2
- proc_zap

As there are multiple proc_foo processes, calling ps.Snapshot("proc_foo") will throw an error. I may update the library to change this, but currently it fits my needs.

This library has been tested on Linux and MacOS, it may not work on windows.

Documentation

http://godoc.org/github.com/umahmood/ps

License

See the LICENSE file for license rights and limitations (MIT).

Documentation

Overview

Package ps which runs 'ps' unix command and returns the process information.

package main

import (
    "fmt"

    "github.com/umahmood/ps"
)

func main() {
    proc, err := ps.Snapshot("firefox")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(proc.PID)
    fmt.Println("")
    fmt.Println(proc)
}

Index

Constants

View Source
const (
	Major = 1
	Minor = 0
	Patch = 0
)

Semantic versioning - http://semver.org/

Variables

View Source
var (
	// ErrEmptyProcName process name is empty
	ErrEmptyProcName = errors.New("ps process name is empty")
	// ErrProcNotFound process not found
	ErrProcNotFound = errors.New("ps process not found")
	// ErrMultipleProcs matched multiple processes
	ErrMultipleProcs = errors.New("ps matched multiple processes")
	// ErrParsingPSOutput error parsing ps output
	ErrParsingPSOutput = errors.New("ps error parsing ps output")
)

Functions

func Version

func Version() string

Version returns library version.

Types

type PS

type PS struct {
	USER    string  // username of the process's owner
	PID     int64   // process ID number
	CPU     float64 // how much of the CPU the process is using
	MEM     float64 // how much memory the process is using
	VSZ     int64   // virtual memory usage
	RSS     int64   // real memory usage
	TTY     string  // terminal associated with the process
	STAT    string  // process status code
	START   string  // time when the process started
	TIME    string  // total CPU usage
	COMMAND string  // name of the process, including arguments, if any
}

PS process information

func Snapshot

func Snapshot(name string) (*PS, error)

Snapshot take a snapshot of the process.

func (PS) String

func (p PS) String() string

String format process information.

Jump to

Keyboard shortcuts

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