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

os/exec: document a method to check if a process is alive #645

Merged
merged 1 commit into from Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/os/exec.go
Expand Up @@ -86,7 +86,9 @@ func Getppid() int { return syscall.Getppid() }
// about the underlying operating system process.
//
// On Unix systems, FindProcess always succeeds and returns a Process
// for the given pid, regardless of whether the process exists.
// for the given pid, regardless of whether the process exists. To test whether
// the process actually exists, see whether p.Signal(syscall.Signal(0)) reports
// an error.
func FindProcess(pid int) (*Process, error) {
return findProcess(pid)
}
Expand Down
18 changes: 18 additions & 0 deletions src/os/exec_unix_test.go
Expand Up @@ -9,6 +9,7 @@ package os_test
import (
"internal/testenv"
. "os"
"syscall"
"testing"
)

Expand All @@ -25,3 +26,20 @@ func TestErrProcessDone(t *testing.T) {
t.Errorf("got %v want %v", got, ErrProcessDone)
}
}

func TestUNIXProcessAlive(t *testing.T) {
testenv.MustHaveGoBuild(t)
t.Parallel()

p, err := StartProcess(testenv.GoToolPath(t), []string{"sleep", "1"}, &ProcAttr{})
if err != nil {
t.Skipf("starting test process: %v", err)
}
defer p.Kill()

proc, _ := FindProcess(p.Pid)
err = proc.Signal(syscall.Signal(0))
if err != nil {
t.Errorf("OS reported error for running process: %v", err)
}
}