/go-ps

:penguin: find, list, and get information from process in linux with Go

Primary LanguageGoMIT LicenseMIT

🐧 go-ps

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

Go Report Card test status

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