Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example code #8

Open
dodona2 opened this issue Jan 7, 2016 · 3 comments
Open

example code #8

dodona2 opened this issue Jan 7, 2016 · 3 comments

Comments

@dodona2
Copy link

dodona2 commented Jan 7, 2016

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)

}

@pfeilbr
Copy link

pfeilbr commented Jun 3, 2016

+1 would be nice to include this in the README

daftaupe pushed a commit to daftaupe/go-ps that referenced this issue Jan 5, 2018
@ahammond
Copy link

I'm new to golang and this really helped.

@stevenspiel
Copy link

stevenspiel commented Mar 14, 2018

slightly refactored (very helpful btw):

main.go:

package main

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

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)
}

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())
	}
}

// FindProcess( key string ) ( int, string, error )
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
}

execution:

$ go get github.com/mitchellh/go-ps
$ go get github.com/pborman/getopt
$ go build main.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants