When executing a process and sending signals to it using: Process.Signal I notice that after sending the second signal syscall.SIGCONT
I got a: os: process already finished
but if using syscall.Kill everything works as expected.
For demonstrative purposes I have created this naive example:
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
"time"
)
func main() {
exit := make(chan error, 1)
go run(exit)
for {
select {
case <-exit:
println("fin, restarting")
run(exit)
default:
time.Sleep(time.Second)
println("running...")
}
}
}
func run(ch chan<- error) {
cmd := exec.Command("sleep", "3")
if err := cmd.Start(); err != nil {
print(err.Error())
os.Exit(1)
}
fmt.Printf("Pid: %d
", cmd.Process.Pid)
go func() {
ch <- cmd.Wait()
}()
time.Sleep(2 * time.Second)
fmt.Printf("%v
", cmd.Process.Signal(syscall.SIGSTOP))
time.Sleep(2 * time.Second)
// Using this will return an os: process already finished
fmt.Printf("%v
", cmd.Process.Signal(syscall.SIGCONT))
// This works as expected
//fmt.Printf("%v
", syscall.Kill(cmd.Process.Pid, syscall.SIGCONT))
}
So basically if using:
cmd.Process.Signal(syscall.SIGCONT)
os: process already finished
is returned
But when using:
syscall.Kill(cmd.Process.Pid, syscall.SIGCONT)
It works as expected.
Could this be a bug on os.exec or it the expected behavior?
UPDATE: seems to be happening only on mac os X