Convert Figma logo to code with AI

mitchellh logogo-ps

Find, list, and inspect processes from Go (golang).

1,470
251
1,470
37

Top Related Projects

14,765

Nomad is an easy-to-use, flexible, and performant workload orchestrator that can deploy a mix of microservice, batch, containerized, and non-containerized applications. Nomad is easy to operate and scale and has native Consul and Vault integrations.

10,470

psutil for golang

Infrastructure as code for DNS!

12,129

:tropical_fish: Beats - Lightweight shippers for Elasticsearch & Logstash

8,819

[Project ended] rkt is a pod-native container engine for Linux. It is composable, secure, and built on standards.

Quick Overview

The mitchellh/go-ps project is a Go library that provides a platform-independent way to list running processes on a system. It supports various operating systems, including Windows, macOS, and Linux.

Pros

  • Platform-Independent: The library works across multiple operating systems, making it a versatile choice for cross-platform applications.
  • Lightweight: The library is lightweight and has minimal dependencies, making it easy to integrate into projects.
  • Active Maintenance: The project is actively maintained, with regular updates and bug fixes.
  • Comprehensive Documentation: The project has detailed documentation, including examples and usage guides.

Cons

  • Limited Functionality: The library provides basic process listing functionality, but may not offer advanced features like process management or control.
  • Potential Performance Impact: Depending on the use case, repeatedly querying process information may have a performance impact on the system.
  • Dependency on External Libraries: The library relies on external libraries for certain platform-specific functionality, which could introduce additional maintenance overhead.
  • Lack of Advanced Features: The library may not provide advanced features like process monitoring, resource usage tracking, or process control.

Code Examples

Here are a few examples of how to use the mitchellh/go-ps library:

  1. Listing All Running Processes:
package main

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

func main() {
    processes, err := ps.Processes()
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    for _, process := range processes {
        fmt.Println(process.Pid(), process.Executable())
    }
}
  1. Filtering Processes by Name:
package main

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

func main() {
    processes, err := ps.Processes()
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    for _, process := range processes {
        if process.Executable() == "firefox" {
            fmt.Println("Found Firefox process with PID:", process.Pid())
        }
    }
}
  1. Getting Process Information:
package main

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

func main() {
    process, err := ps.FindProcess(123)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Println("Process ID:", process.Pid())
    fmt.Println("Executable:", process.Executable())
}

Getting Started

To use the mitchellh/go-ps library in your Go project, follow these steps:

  1. Install the library using the Go package manager:
go get github.com/mitchellh/go-ps
  1. Import the library in your Go file:
import "github.com/mitchellh/go-ps"
  1. Use the provided functions to interact with the process information. For example, to list all running processes:
processes, err := ps.Processes()
if err != nil {
    // Handle the error
}

for _, process := range processes {
    fmt.Println(process.Pid(), process.Executable())
}
  1. Refer to the project's documentation for more advanced usage and examples.

Competitor Comparisons

14,765

Nomad is an easy-to-use, flexible, and performant workload orchestrator that can deploy a mix of microservice, batch, containerized, and non-containerized applications. Nomad is easy to operate and scale and has native Consul and Vault integrations.

Pros of Nomad

  • Nomad is a full-featured, production-ready container orchestration platform that can handle a wide range of workloads, including Docker, Podman, and more.
  • Nomad provides advanced scheduling and resource management features, making it a powerful tool for running complex, distributed applications.
  • Nomad is part of the HashiCorp ecosystem, which means it integrates well with other HashiCorp tools like Vault and Consul.

Cons of Nomad

  • Nomad has a steeper learning curve compared to go-ps, as it is a more complex and feature-rich tool.
  • Nomad requires more resources to run, as it is a full-fledged orchestration platform, while go-ps is a lightweight library.

Code Comparison

go-ps (mitchellh/go-ps):

func processes() ([]Process, error) {
    dir, err := os.Open("/proc")
    if err != nil {
        return nil, err
    }
    defer dir.Close()

    names, err := dir.Readdirnames(-1)
    if err != nil {
        return nil, err
    }
}

Nomad (hashicorp/nomad):

func (c *Client) Run(ctx context.Context, job *Job) (*Evaluation, *ClientError) {
    resp, err := c.apiRequest(ctx, "POST", "/v1/jobs", job)
    if err != nil {
        return nil, NewClientError(err)
    }
    defer resp.Body.Close()

    var evalResp Evaluation
    if err := decodeBody(resp, &evalResp); err != nil {
        return nil, NewClientError(err)
    }

    return &evalResp, nil
}
10,470

psutil for golang

Pros of shirou/gopsutil

  • Provides a wider range of system information, including CPU, memory, disk, network, and process data.
  • Supports multiple operating systems, including Windows, Linux, and macOS.
  • Offers a more comprehensive set of functions for system monitoring and management.

Cons of shirou/gopsutil

  • May have a larger codebase and dependencies compared to mitchellh/go-ps.
  • May have a steeper learning curve due to the broader set of features and functionality.
  • May have slightly lower performance for specific use cases where mitchellh/go-ps is more optimized.

Code Comparison

mitchellh/go-ps:

func Processes() ([]Process, error) {
    return processes()
}

type Process struct {
    Pid  int
    PPid int
}

shirou/gopsutil:

func Processes() ([]Process, error) {
    return processes()
}

type Process struct {
    Pid             int32
    PPid            int32
    Name           string
    Exe            string
    Cmdline        string
    CreateTime     int64
    Cwd            string
    Root           string
    Status         string
    Username       string
    Nice          int32
    Threads        int32
    CPUTimes       CPUTimesStat
    MemInfo        MemoryInfoStat
    IOCounters     IOCountersStat
    NumCtxSwitches NumCtxSwitchesStat
    NumFDs         int32
    NumThreads     int32
    Connections    []net.ConnectionStat
}

Infrastructure as code for DNS!

Pros of dnscontrol

  • dnscontrol supports a wide range of DNS providers, making it a versatile tool for managing DNS configurations across multiple platforms.
  • dnscontrol provides a declarative syntax for defining DNS configurations, making it easier to manage and version control DNS settings.
  • dnscontrol includes built-in support for various DNS record types, such as A, CNAME, MX, and TXT records.

Cons of dnscontrol

  • dnscontrol may have a steeper learning curve compared to go-ps, as it requires understanding the declarative syntax and configuration file structure.
  • dnscontrol may have a larger codebase and dependencies, which could make it less lightweight and more resource-intensive than go-ps.

Code Comparison

go-ps:

func FindProcess(pid int) (Process, error) {
    p, err := os.FindProcess(pid)
    if err != nil {
        return nil, err
    }

    return &UnixProcess{Pid: pid, process: p}, nil
}

dnscontrol:

func (c *DNSControl) GetDomainCorrections(domain string) ([]*Correction, error) {
    corrections := []*Correction{}
    dc, err := c.Domains.LoadDomainConfig(domain)
    if err != nil {
        return nil, err
    }

    // Perform various checks and generate corrections
    // ...

    return corrections, nil
}
12,129

:tropical_fish: Beats - Lightweight shippers for Elasticsearch & Logstash

Pros of Elastic/Beats

  • Elastic/Beats is a more comprehensive and feature-rich monitoring solution, providing a wide range of data collection agents for various systems and applications.
  • The project has a large and active community, with extensive documentation and support resources available.
  • Elastic/Beats integrates seamlessly with the Elastic Stack, allowing for powerful data analysis and visualization capabilities.

Cons of Elastic/Beats

  • Elastic/Beats may have a steeper learning curve compared to the more lightweight and focused mitchellh/go-ps.
  • The project's complexity and feature set may be overkill for users with simpler monitoring requirements.
  • Elastic/Beats may have a higher resource footprint, particularly on systems with limited resources.

Code Comparison

mitchellh/go-ps:

func Processes() ([]Process, error) {
    var processes []Process

    files, err := ioutil.ReadDir("/proc")
    if err != nil {
        return nil, err
    }

    for _, f := range files {
        if f.IsDir() && isInt(f.Name()) {
            p, err := newProcess(mustAtoi(f.Name()))
            if err == nil {
                processes = append(processes, p)
            }
        }
    }

    return processes, nil
}

Elastic/Beats:

func (b *Beat) Run(bt beat.Beater) error {
    err := b.Init()
    if err != nil {
        return err
    }

    // Register sigHandler
    b.registerSignalHandlers()

    // Marked as running
    b.isRunning = true
    defer func() { b.isRunning = false }()

    return bt.Run(b)
}
8,819

[Project ended] rkt is a pod-native container engine for Linux. It is composable, secure, and built on standards.

Pros of rkt

  • rkt is a container runtime that provides a more secure and flexible alternative to Docker.
  • rkt supports a wide range of container image formats, including Docker, OCI, and appc.
  • rkt has a strong focus on security, with features like support for SELinux and AppArmor.

Cons of rkt

  • rkt has a smaller community and ecosystem compared to Docker, which may make it harder to find support and resources.
  • rkt has a steeper learning curve than Docker, especially for users who are already familiar with Docker.
  • rkt may not be as widely adopted as Docker, which could limit its long-term viability.

Code Comparison

Here's a brief comparison of the code for the ps function in mitchellh/go-ps and the rkt command in rkt/rkt:

mitchellh/go-ps:

func ps() ([]Process, error) {
    pids, err := processes()
    if err != nil {
        return nil, err
    }

    var procs []Process
    for _, pid := range pids {
        proc, err := newProcess(pid)
        if err != nil {
            return nil, err
        }
        procs = append(procs, proc)
    }

    return procs, nil
}

rkt/rkt:

func runRkt(args []string) error {
    cmd := exec.Command(rktBinPath, args...)
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    return cmd.Run()
}

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Process List Library for Go GoDoc

go-ps is a library for Go that implements OS-specific APIs to list and manipulate processes in a platform-safe way. The library can find and list processes on Linux, Mac OS X, Solaris, and Windows.

If you're new to Go, this library has a good amount of advanced Go educational value as well. It uses some advanced features of Go: build tags, accessing DLL methods for Windows, cgo for Darwin, etc.

How it works:

  • Darwin uses the sysctl syscall to retrieve the process table.
  • Unix uses the procfs at /proc to inspect the process tree.
  • Windows uses the Windows API, and methods such as CreateToolhelp32Snapshot to get a point-in-time snapshot of the process table.

Installation

Install using standard go get:

$ go get github.com/mitchellh/go-ps
...

TODO

Want to contribute? Here is a short TODO list of things that aren't implemented for this library that would be nice:

  • FreeBSD support
  • Plan9 support