无法在执行exec.Command时从操作系统级别查看命令参数。

huangapple go评论86阅读模式
英文:

unable to see command arguments at OS level issued from exec.Command

问题

我的例程应该从相同的go可执行二进制文件(os.Args[0])中启动10个子进程,并添加一些有效的命令行参数。所有进程应该在指定的秒数内运行,该秒数在其中一个参数中指定。

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() {
    // 不显示解析持续时间参数的代码
    // 创建10个子进程
    go spinChildProcesses()
    // 设置进程的持续时间并终止
    time.Sleep(time.Second * time.Duration(duration))
    fmt.Println(" - 进程正常终止")
}

当运行上述代码时,查看操作系统级别,可以看到参数没有传递。只有我键入的根进程具有参数:

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

有什么想法为什么参数没有传递给子进程,以及如何确保参数传递给子进程?

英文:

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] = &quot;-c&quot;
	cmdParts[1] = os.Args[0]
	cmdParts[2] = &quot;--duration&quot;
	cmdParts[3] = &quot;10000&quot;
	for i := 0; i &lt; 10; i++ {
		proc := exec.Command(&quot;bash&quot;, 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(&quot; - process terminating normaly&quot;)
}

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 ?

答案1

得分: 3

-c bash标志接受一个字符串参数进行解释。由于-c的参数只是字符串os.Args[0],所以bash只执行这个字符串,而忽略了其他参数。

要将要执行的二进制文件的参数提供给bash -c,请将它们连接成一个字符串:

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", strings.Join(args, " "))
    go proc.Start()
}

或者直接执行你的二进制文件,而不需要额外的shell。

英文:

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, &quot;--duration&quot;)
args = append(args, &quot;10000&quot;)
for i := 0; i &lt; 10; i++ {
	proc := exec.Command(&quot;/bin/bash&quot;, &quot;-c&quot;, stringsJoin(args, &quot; &quot;))
	go proc.Start()
}

Or simply exec your binary directly without the extra shell.

huangapple
  • 本文由 发表于 2016年10月24日 23:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/40222222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定