Skip to main content
The Process struct provides numerous methods to retrieve detailed information about a process including its name, command line, status, executable path, and more.

Name

Function Signature

func (p *Process) Name() (string, error)
func (p *Process) NameWithContext(ctx context.Context) (string, error)
Returns the process name (usually the executable filename without path).

Example

p, _ := process.NewProcess(1234)
name, err := p.Name()
if err != nil {
    panic(err)
}
fmt.Printf("Process name: %s\n", name)
// Output: Process name: nginx

Cmdline

Function Signature

func (p *Process) Cmdline() (string, error)
func (p *Process) CmdlineWithContext(ctx context.Context) (string, error)
Returns the complete command line used to start the process as a single string with arguments separated by spaces (0x20).

Example

p, _ := process.NewProcess(1234)
cmdline, err := p.Cmdline()
if err != nil {
    panic(err)
}
fmt.Printf("Command line: %s\n", cmdline)
// Output: Command line: /usr/sbin/nginx -c /etc/nginx/nginx.conf

CmdlineSlice

Function Signature

func (p *Process) CmdlineSlice() ([]string, error)
func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error)
Returns the command line arguments as a slice with each element being a separate argument.

Example

p, _ := process.NewProcess(1234)
args, err := p.CmdlineSlice()
if err != nil {
    panic(err)
}

fmt.Println("Command line arguments:")
for i, arg := range args {
    fmt.Printf("  [%d]: %s\n", i, arg)
}
// Output:
// Command line arguments:
//   [0]: /usr/sbin/nginx
//   [1]: -c
//   [2]: /etc/nginx/nginx.conf
On Windows, this assumes the command line follows the convention accepted by CmdlineToArgv. Use Cmdline() if you need custom parsing.

Status

Function Signature

func (p *Process) Status() ([]string, error)
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error)
Returns the current process status. The return value is a slice that may contain one or more status strings.

Status Constants

  • Running - Running or runnable (on run queue)
  • Sleep - Sleeping (interruptible wait)
  • Stop - Stopped process
  • Zombie - Defunct process
  • Blocked - Waiting on I/O
  • Idle - Idle (sleeping for >20 seconds)
  • Lock - Waiting to acquire a lock
  • Wait - Idle interrupt thread

Example

p, _ := process.NewProcess(1234)
status, err := p.Status()
if err != nil {
    panic(err)
}

fmt.Printf("Process status: %v\n", status)
// Output: Process status: [running]

if len(status) > 0 && status[0] == process.Running {
    fmt.Println("Process is running")
}

Exe

Function Signature

func (p *Process) Exe() (string, error)
func (p *Process) ExeWithContext(ctx context.Context) (string, error)
Returns the absolute path to the process executable.

Example

p, _ := process.NewProcess(1234)
exePath, err := p.Exe()
if err != nil {
    panic(err)
}
fmt.Printf("Executable: %s\n", exePath)
// Output: Executable: /usr/sbin/nginx

Cwd

Function Signature

func (p *Process) Cwd() (string, error)
func (p *Process) CwdWithContext(ctx context.Context) (string, error)
Returns the current working directory of the process.

Example

p, _ := process.NewProcess(1234)
cwd, err := p.Cwd()
if err != nil {
    panic(err)
}
fmt.Printf("Working directory: %s\n", cwd)
// Output: Working directory: /var/www

Username

Function Signature

func (p *Process) Username() (string, error)
func (p *Process) UsernameWithContext(ctx context.Context) (string, error)
Returns the username of the process owner.

Example

p, _ := process.NewProcess(1234)
username, err := p.Username()
if err != nil {
    panic(err)
}
fmt.Printf("Owner: %s\n", username)
// Output: Owner: www-data

Uids & Gids

Function Signature

func (p *Process) Uids() ([]uint32, error)
func (p *Process) Gids() ([]uint32, error)
func (p *Process) Groups() ([]uint32, error)
Return user IDs, group IDs, and supplementary group IDs of the process.

Example

p, _ := process.NewProcess(1234)

uids, _ := p.Uids()
fmt.Printf("User IDs: %v\n", uids)
// Output: User IDs: [33 33 33 33] (real, effective, saved, filesystem)

gids, _ := p.Gids()
fmt.Printf("Group IDs: %v\n", gids)

groups, _ := p.Groups()
fmt.Printf("Supplementary groups: %v\n", groups)

Process Relationships

Ppid

func (p *Process) Ppid() (int32, error)
func (p *Process) PpidWithContext(ctx context.Context) (int32, error)
Returns the parent process ID.
p, _ := process.NewProcess(1234)
ppid, err := p.Ppid()
if err != nil {
    panic(err)
}
fmt.Printf("Parent PID: %d\n", ppid)

Parent

func (p *Process) Parent() (*Process, error)
func (p *Process) ParentWithContext(ctx context.Context) (*Process, error)
Returns a Process instance for the parent process.
p, _ := process.NewProcess(1234)
parent, err := p.Parent()
if err != nil {
    panic(err)
}

parentName, _ := parent.Name()
fmt.Printf("Parent process: %s (PID %d)\n", parentName, parent.Pid)

Children

func (p *Process) Children() ([]*Process, error)
func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error)
Returns a slice of Process instances for all child processes.
p, _ := process.NewProcess(1234)
children, err := p.Children()
if err != nil {
    panic(err)
}

fmt.Printf("Child processes: %d\n", len(children))
for _, child := range children {
    name, _ := child.Name()
    fmt.Printf("  PID %d: %s\n", child.Pid, name)
}

Process Timing

CreateTime

func (p *Process) CreateTime() (int64, error)
func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error)
Returns the process creation time in milliseconds since Unix epoch.
p, _ := process.NewProcess(1234)
createTime, err := p.CreateTime()
if err != nil {
    panic(err)
}

startTime := time.Unix(0, createTime*int64(time.Millisecond))
fmt.Printf("Started: %s\n", startTime.Format(time.RFC3339))
fmt.Printf("Running for: %s\n", time.Since(startTime).Round(time.Second))

IsRunning

func (p *Process) IsRunning() (bool, error)
func (p *Process) IsRunningWithContext(ctx context.Context) (bool, error)
Checks if the process is still running. This is more reliable than just checking if the PID exists, as it also verifies the create time hasn’t changed (which would indicate PID reuse).
p, _ := process.NewProcess(1234)

running, err := p.IsRunning()
if err != nil {
    panic(err)
}

if running {
    fmt.Println("Process is running")
} else {
    fmt.Println("Process has terminated")
}

Environment Variables

Environ

func (p *Process) Environ() ([]string, error)
func (p *Process) EnvironWithContext(ctx context.Context) ([]string, error)
Returns the environment variables of the process as a slice of “KEY=VALUE” strings.
p, _ := process.NewProcess(1234)
envVars, err := p.Environ()
if err != nil {
    panic(err)
}

for _, env := range envVars {
    fmt.Println(env)
}
// Output:
// PATH=/usr/local/bin:/usr/bin:/bin
// HOME=/root
// USER=root

Comprehensive Example

package main

import (
    "fmt"
    "time"
    "github.com/shirou/gopsutil/v4/process"
)

func printProcessInfo(pid int32) {
    p, err := process.NewProcess(pid)
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }
    
    // Basic info
    name, _ := p.Name()
    cmdline, _ := p.Cmdline()
    status, _ := p.Status()
    exe, _ := p.Exe()
    cwd, _ := p.Cwd()
    
    fmt.Printf("Process Information for PID %d\n", pid)
    fmt.Printf("=================================\n")
    fmt.Printf("Name: %s\n", name)
    fmt.Printf("Executable: %s\n", exe)
    fmt.Printf("Working Dir: %s\n", cwd)
    fmt.Printf("Command Line: %s\n", cmdline)
    fmt.Printf("Status: %v\n", status)
    
    // User info
    username, _ := p.Username()
    uids, _ := p.Uids()
    gids, _ := p.Gids()
    fmt.Printf("\nUser: %s (UID: %v, GID: %v)\n", username, uids, gids)
    
    // Timing
    createTime, _ := p.CreateTime()
    startTime := time.Unix(0, createTime*int64(time.Millisecond))
    fmt.Printf("\nStarted: %s\n", startTime.Format(time.RFC3339))
    fmt.Printf("Running for: %s\n", time.Since(startTime).Round(time.Second))
    
    // Parent/Children
    ppid, _ := p.Ppid()
    children, _ := p.Children()
    fmt.Printf("\nParent PID: %d\n", ppid)
    fmt.Printf("Child processes: %d\n", len(children))
    
    // Threads
    numThreads, _ := p.NumThreads()
    fmt.Printf("Threads: %d\n", numThreads)
}

func main() {
    printProcessInfo(1) // Init process
}

Platform Notes

Reads from /proc/[pid]/ files including cmdline, status, exe, cwd symlinks.

Error Handling

Many methods may return ErrorNotPermitted when trying to access information about processes owned by other users, especially on Unix systems. Run with appropriate privileges or handle this error gracefully.

Memory Info

Get memory usage statistics

CPU Usage

Get CPU usage information

I/O Counters

Get I/O statistics

Overview

Back to process package overview

Build docs developers (and LLMs) love