3
votes

I have the following code in Go:

cmd := exec.Command(...)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Run()

Which spawns another Go program as a child process and sets its Stdin, Stdout, and Stderr to that of the parent. After the child is spawned, I want to kill the parent via os.Exit() in the parent process or syscall.Kill(os.Getppid(), syscall.SIGTERM) in the child. The problem arises after I kill the parent. It seems to close Stdin, so the child can't receive input from the terminal where the parent was spawned.

I can receive input on the child before I kill the parent, so it's definitely the action of killing the parent that closes Stdin. Is there any way I can work around this?

1
Does the parent need to do something after starting the child? Normally this is what you'd use the execve syscall for (syscall.Exec in go).JimB
Using syscall.Exec was the answer! Thanks very much.Dan Doe

1 Answers

0
votes

From my experience, I do not know of a way to get around that for Stdin. Only Stdout and Stderr works that way.

An alternative is to free the resources being used by the parent process (i.e. to leave it as light as possible) and let it wait for the execution of the child process.