Giter VIP home page Giter VIP logo

go-ps's Introduction

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

go-ps's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-ps's Issues

Releases + Tags

Hey there.

Is it possible to do releases or tags here?

Love, someone having to maintain a Gopkg.toml

Out-of-memory during Process32NextW returns no error and truncated process list

Hi,

The loop in https://github.com/mitchellh/go-ps/blob/master/process_windows.go#L112 can return false for (A) successfully reaching the end of the list, or (B) some other error.

As per https://docs.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32-process32nextw we must check GetLastError() for ERROR_NO_MORE_FILES after exiting this loop.

I encountered this problem today, on a PC under memory pressure, where this function returned a truncated process list.

WSL: Show native Windows processes

I have a need to show the complete list of processes, so that Go code running inside Windows Subsystem for Linux reports not only the Linux processes, but the native Windows processes as well. Could we please expand the list?

For example, detect when the environment is WSL. Then run /mnt/c/Windows/System32/tasklist.exe and parse the results.

example code

Proposal:

// -- compile-command: "go build main.go" --
// https://golang.org/pkg/errors/#pkg-index
package main

import (
"errors"
"fmt"
"os"
"github.com/mitchellh/go-ps"
"github.com/pborman/getopt"
)

const ()

func PS(){
ps, _ := ps.Processes()
fmt.Println(ps[0].Executable())
for pp, _ := range(ps){
fmt.Printf("%d %s\n", ps[pp].Pid(),ps[pp].Executable())
}
}

func FindProcess(key string) (int, string, error) {
pname := ""
pid := 0
err := errors.New("not found")
ps, _ := ps.Processes()
for i, _ := range ps {
if ps[i].Executable() == key {
pid = ps[i].Pid()
pname = ps[i].Executable()
err = nil
break
}
}
return pid, pname, err
} // FindProcess( key string ) ( int, string, error )

func main() {
// exit code
rc := 0
// values from command line
optHelp := getopt.BoolLong("help", 0, "Help")
getopt.Parse()
if *optHelp {
getopt.Usage()
os.Exit(rc)
}
if pid, s, err := FindProcess("emacs"); err == nil {
fmt.Printf ("Pid:%d, Pname:%s\n", pid, s)
}

fmt.Println("")
os.Exit(rc)

}

Support cmdline under linux?

There are scenarios where people want to look at the actual command of a process, especially when there are multiple processes with same binary but using different parameters

package not ps

when using
go get github.com/mitchellh/go-ps,
the package is placed in
~/go/src/github.com/mitchellh/go-ps
however the package is ps.
I fixed it locally by changing the directory to gops and modifying the package in the each file.

Offer process termination

Hi,

I am happy to report that process querying is working well with go-ps.

Could we add a portable Terminate() function to the process interface? This can help developers to cleanup after stuck processes in a platform independent way.

Fetching Top Five Process ID's

Hi @mitchellh ,

Thanks for such a nice Library.Before this plugin i was trying for the following approaches.

1)Calling the C code which does the Command based logic in getting the top process.
2)Execute a os command and fetch the values and iterate them

Here is the sample output for 5 top process running on my pc
vagrant@vagrant-ubuntu-trusty-64:~/$ ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 5|awk '{print $1}'
1812
1847
925
2232
2286

Is there a possibility that i can do this through the library.I see there are two important functions available where one gets all the porcess pid's running on the machine and the other gets the information of a specific PID.

I was thinking if there is a way or a logic if i can get the top five process running from the library as a function.

Thanks again for the library
Vamsi KGR

Add Pids() method

I have handmade linux implementation that for specific pid returns it child pids.
Also i'm using serach by process name to get all pids for this process name mask.
I can create pr for this (start from linux and may be freebsd) does it will be useful ?

Not showing 32 bit processes on 64 bit Windows

I wrote small Windows app that writes down list of processes, but it is shorter than what TaskManager shows. It seems that processes that are marked as "process name (32)" in TaskManager are not "visible" to go-ps.

Executable names corrupted on Linux

Executable names are wonky. For example, /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal is truncated to unattended-upgr.

unattended-upgr is not the real executable name. It's not even a filename. It's an incomplete part of the directory name supplied as a command line argument.

Incomplete process names from wine

I am trying to make a "super alt f4" for linux, which kills the current active window when you press alt and f4 at the same time, however this package returns incomplete name for some processes such as league of legends running from wine.

Executable() in ps.Processes does not return the full process name

Code

package main

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

func main() {

    if p, err := ps.Processes(); err != nil {
        log.Fatal(err)
    } else {
        for _, v := range p {
            fmt.Println(v.Executable())
        }
    }
}

Example Truncated process: NetworkBrowserAg

Is this a limitation of the system call sysctl or the implementation of the darwinProcesses() C function? It would be nice to have the full process name in order to instill confidence when selecting processes by name.

This tool will be largely helpful for systems /(administrators|engineers)/ who want to move to Go from the standard interpreted variety (which is great because packaging and deploying is easier). If the full process name is available, I wager that go-ps would be a bit more practical! What do you think?

Not an issue, just some thanks.

Hi,
I've wrote a simple archiving program in golang and i use a part of your code for getting process list and warn user when a predefined process is actually running.
Work like a charm, many thanks.
This utility will be available as MIT license like your own code.
Not published at this time (i need some times to use it and to decide it's safe to use without any damageable bugs) .
Have nice days and thanks again.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.