Reputation: 10799
Consider the following Go code fragment:
cmd := exec.Command(program, arg0)
stdin, err := cmd.StdinPipe()
// produces error when b is too large
n, err := stdin.Write(b.Bytes())
Whenever b
is too large, Write()
returns an error. Having experimented with different size b
s, it would seem this occurs whenever the length of b
is longer than the Linux pipe buffer size. Is there a way around this? Essentially I need to feed large log files via stdin to an external script.
Upvotes: 2
Views: 572
Reputation: 92984
I wrote this program to test your code:
package main
import "os/exec"
import "fmt"
func main() {
cmd := exec.Command("/bin/cat")
in, _ := cmd.StdinPipe()
cmd.Start()
for i := 1024*1024; ; i += 1024*1024 {
b := make([]byte,i)
n, err := in.Write(b)
fmt.Printf("%d: %v\n", n, err)
if err != nil {
cmd.Process.Kill()
return
}
}
}
The only way this program gives an error is if the called process closes stdin. Does the program you call close stdin? This might be a bug in the Go runtime.
Upvotes: 1