Reputation: 8885
My routine is supposed to spin 10 child processes from the same go executable binary (os.Args[0]), adding some command line arguments that are valid. All processes should live for a number of seconds, which is specified in one of the arguments.
func spinChildProcesses() {
cmdParts := make([]string, 4)
cmdParts[0] = "-c"
cmdParts[1] = os.Args[0]
cmdParts[2] = "--duration"
cmdParts[3] = "10000"
for i := 0; i < 10; i++ {
proc := exec.Command("bash", cmdParts...)
go proc.Start()
}
}
func main() {
# not showing code that parses duration arg
# create 10 child subprocesses
go spinChildProcesses()
// set a duration to the process and terminate
time.Sleep(time.Second * time.Duration(duration))
fmt.Println(" - process terminating normaly")
}
When the above is run, looking at OS level I can see the arguments are not carried out. Only the root process has the arguments which I typed:
ps -ef | grep my-test-pr
root 3806 14446 0 15:23 pts/1 00:00:00 ./my-test-pr --duration 10000
root 3810 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3811 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3813 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3814 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3818 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3823 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3824 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3829 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3836 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
root 3840 3806 0 15:23 pts/1 00:00:00 ./my-test-pr
Any idea why and how to ensure the arguments are passed to the children processes ?
Upvotes: 0
Views: 114
Reputation: 109347
The -c
bash flag takes a single string argument to interpret. Since the argument to -c
is only the string os.Args[0]
, that is all bash is executing, and the rest of the args are being ignored.
To provide the arguments to your binary to be executed by bash -c
, join them into a single string:
var args []string
args = append(args, os.Args[0])
args = append(args, "--duration")
args = append(args, "10000")
for i := 0; i < 10; i++ {
proc := exec.Command("/bin/bash", "-c", stringsJoin(args, " "))
go proc.Start()
}
Or simply exec your binary directly without the extra shell.
Upvotes: 3