项目作者: umahmood

项目描述 :
ps a Go library which runs the 'ps' unix command and returns the process information.
高级语言: Go
项目地址: git://github.com/umahmood/ps.git
创建时间: 2019-01-01T18:22:29Z
项目社区:https://github.com/umahmood/ps

开源协议:MIT License

下载


PS

A Go library which runs the Unix ‘ps’ command and returns structured information
about the process.

This library essentially runs:

  1. bash -c ps -aux | grep [p]rocess_name

And parses the output of the command.

Installation

$ go get github.com/umahmood/ps

Usage

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/umahmood/ps"
  5. )
  6. func main() {
  7. proc, err := ps.Snapshot("firefox")
  8. if err != nil {
  9. fmt.Println(err)
  10. return
  11. }
  12. fmt.Println(proc.PID)
  13. fmt.Println("")
  14. fmt.Println(proc)
  15. }

Output:

  1. 19352
  2. User : usman
  3. PID : 19352
  4. CPU : 0.00
  5. MEM : 8.10
  6. VSZ : 8353516
  7. RSS : 1350828
  8. TTY : ??
  9. STAT : S
  10. START : 22Dec18
  11. TIME : 334:44.58
  12. COMMAND: /Applications/Firefox.app/Contents/MacOS/firefox

What do the output fields of the ps command mean?

Limitations

The library only matches unique running processes. If there are multiple processes
with the same name, then an error is returned. i.e. if we have the following
running processes:

  1. - proc_foo --arg=1
  2. - proc_foo --arg=2
  3. - proc_zap

As there are multiple proc_foo processes, calling ps.Snapshot("proc_foo") will
throw an error. I may update the library to change this, but currently it fits
my needs.

This library has been tested on Linux and MacOS, it may not work on windows.

Documentation

http://godoc.org/github.com/umahmood/ps

License

See the LICENSE file for license rights and limitations (MIT).