ps

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: MIT Imports: 6 Imported by: 0

README

go-ps

Go Report Card test status

go-ps is process library for find, list, and get information from process. go-ps read information about process from /proc file.

Documentation

see pkg.go.dev

Installation

$ go get github.com/nothinux/go-ps
Getting Started
Get All Running Process Name
package main

import (
    "log"
    "fmt"
    "github.com/nothinux/go-ps"
)

func main() {
    process, err := ps.GetProcess()
    if err != nil {
        log.Fatal(err)
    }

    for _, p := range process {
        fmt.Println(p.Comm)
    }
}
Find Pid from Process Name
package main

import (
    "log"
    "github.com/nothinux/go-ps"
    "fmt"
)

func main() {
    pid, err := ps.FindPid("nginx")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(pid)
}
Find Process then get information from that process
package main


import (
    "log"
    "github.com/nothinux/go-ps"
    "fmt"
)

func main() {
    p, err := ps.FindProcessName("nginx")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("process id:", p.Pid)
    fmt.Println("process name:", p.Comm)
    fmt.Println("process cmd:", p.CmdLine)
    fmt.Println("process state:", p.State)

}

more

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoProcess = errors.New("no process with provided name found")
)

Functions

func FindPGid added in v0.2.0

func FindPGid(name string) (int, error)

FindPGid returns process group id from given process name

func FindPid added in v0.2.0

func FindPid(name string) (int, error)

FindPid returns process id from given process name

func FindPpid

func FindPpid(name string) (int, error)

FindPpid returns parent process id from given process name

func GetPidState added in v0.2.0

func GetPidState(pid int) (string, error)

GetPidState returns state for given process id

func GetPids

func GetPids() ([]int, error)

GetPids returns a slice of process ID

func PidIsExists added in v0.2.0

func PidIsExists(pid int) (bool, error)

ProcessIsExists return true if given pid is exists

func ProcessIsExists added in v0.2.0

func ProcessIsExists(name string) (bool, error)

ProcessIsExists return true if given process name is exists

func StateToString added in v0.2.0

func StateToString(state string) string

StateToString returns state representation for given state

Types

type Process

type Process struct {
	// The process id
	Pid int
	// The executable filename of running process
	Comm string
	// Full command line of running process
	CmdLine string
	// state of this process
	State string
	// The parent process id from this process
	Ppid int
	//  The process group id from this process
	Pgrp int
	// The User id and group id who running process
	UID, GID int
}

Process contains information about running process

func FindProcess added in v0.2.0

func FindProcess(pid int) (Process, error)

FindProcess returns Process struct from given process id

func FindProcessName added in v0.3.0

func FindProcessName(name string) (Process, error)

FindProcessName returns Process based on finding, it will be match if given process name same with executable filename

func FindProcessNameContains added in v0.3.0

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

FindProcessNameContains returns slice of Process based on finding, it will returns process containing name. Using this is not very accurate, if you want to search process based on executable file name, use FindProcessName instead

func GetProcess

func GetProcess() ([]Process, error)

GetProcess returns all process information pid, comm, ppid, pgrp

Jump to

Keyboard shortcuts

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