Home > Back-end >  why golang exec can't get java pid
why golang exec can't get java pid

Time:11-06

Linux shell get java pid

$pgrep -u admin java
1866

go code test.go

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    ret, err := exec.Command("pgrep", "-u ", "admin", "java").CombinedOutput()
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(ret))

}

go run test.go

$go run test.go
exit status 2

the question is why linux shell can get java pid ,but go call exec can't get java pid

the go version :

$go version
go version go1.4.2 linux/amd64

the os info

redhat 7.0 

CodePudding user response:

Try to use "-u" instead of "-u ".

CodePudding user response:

That looks like "exec.Command cannot find pgrep". You could check this by adding something like the following:

path, err := exec.LookPath("pgrep")
if err != nil {
    fmt.Printf("Could not find pgrep, err: %v\n", err)
} else {
    fmr.Printf("The path to pgrep is %s\n", path)
}
  •  Tags:  
  • go
  • Related