I wrote a function to run process, this is my code:
func execCmd(user User, command string) (*exec.Cmd, error) {
cmd := exec.Command(os.Getenv("SHELL"), "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(user.UID), Gid: uint32(user.GID)}
if err := cmd.Start(); err != nil {
return nil, err
}
return cmd, nil
}
I used cmd.Process.Pid
to get the pid of the process after the process has started. It works well with a command like /tmp/test
but returns an unexpected pid with the command /tmp/test > /tmp/test.log
. The command returns 1 less than the actual pid (actual pid - 1
). I want to get the pid /tmp/test
and after use this pid kill /tmp/test
.
For example: cmd.Process.pid = 10667
, but ps -ef | grep /tmp/test
shows that the pid equals 10668
Thanks.