27

How can I get the list of currently running processes in Go?

The OS package provides some functions: http://golang.org/pkg/os/ but doesn't give anything to see the list of running processes.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
Kunal P.Bharati
  • 949
  • 4
  • 11
  • 19

6 Answers6

24

There is no such function in the standard library and most likely never will be.

In most cases, the list of processes isn't required by programs. Go programs usually want to wait for a single or a smaller number of processes, not for all processes. PIDs of processes are usually obtained by other means than searching the list of all processes.

If you are on Linux, the list of processes can be obtained by reading contents of /proc directory. See question Linux API to list running processes?

Community
  • 1
  • 1
21

This library: github.com/mitchellh/go-ps worked for me.

import (
      ps "github.com/mitchellh/go-ps"
      ... // other imports here...
)

func whatever(){
    processList, err := ps.Processes()
    if err != nil {
        log.Println("ps.Processes() Failed, are you using windows?")
        return
    }

    // map ages
    for x := range processList {
        var process ps.Process
        process = processList[x]
        log.Printf("%d\t%s\n",process.Pid(),process.Executable())

        // do os.* stuff on the pid
    }
}
Felipe Valdes
  • 1,998
  • 15
  • 26
10

I suggest to use for this purpose the following library: https://github.com/shirou/gopsutil/

Here is an example to get total processes and running ones:

package main

import (
    "fmt"
    "github.com/shirou/gopsutil/host"
    "github.com/shirou/gopsutil/load"
)

func main() {
    infoStat, _ := host.Info()
    fmt.Printf("Total processes: %d\n", infoStat.Procs)

    miscStat, _ := load.Misc()
    fmt.Printf("Running processes: %d\n", miscStat.ProcsRunning)
}

The library allows to get several other data. Take a look at the documentation for available informations provided according to the target operative system.

gregorycallea
  • 1,218
  • 1
  • 9
  • 28
  • As per OP's request to see the list of processes (vs. just the count): "github.com/shirou/gopsutil/process" ... processes, err := process.Processes() – SVUser Jun 08 '21 at 01:54
5

If you only need the process information, can just run "ps" command from your go code, then parse the text output.

A complete solution can refer to Exercise 29 in Book "Learning Go" @ http://www.miek.nl/files/go/

yongzhy
  • 979
  • 8
  • 18
4

you can use this library github.com/shirou/gopsutil

package main
import (
    "fmt"
    "github.com/shirou/gopsutil/v3/process"
)

func main() {
    processes, _ := process.Processes()
    for _, process := range processes {
        name, _ := process.Name()
        fmt.Println(name)
    }
}

in this library,you can also get process info other

shiwei xia
  • 41
  • 1
2

For linux

I found a fairly simple solution to get the list of running processes without using a large library:

package main

import (
    "fmt"
    "os"
    "path/filepath"
)

func main() {
    matches, err := filepath.Glob("/proc/*/exe")
    for _, file := range matches {
        target, _ := os.Readlink(file)
        if len(target) > 0 {
            fmt.Printf("%+v\n", target)
        }
    }
}

It will print the path for each running process. If you need just the process name, then you can get it with filepath.Base(target)

This works by de-referencing the symlink for the /proc/[procID]/exe file, which is a symlink to the executable file. This is much simpler than reading and extracting the process name from the /proc/[procID]/status file (as suggested in other solutions I found).

PS: This might not work on all distribution because it relies on the exe file in the process' folder, which might not present in all flavors of Linux.

BenVida
  • 1,796
  • 1
  • 16
  • 25