how can I get stdin to exec cmd in golang

后端 未结 4 1004
故里飘歌
故里飘歌 2021-02-07 06:32

I have this code

subProcess := exec.Cmd{
    Path: execAble,
    Args: []string{
        fmt.Sprintf(\"-config=%s\", *configPath),
        fmt.Sprintf(\"-serverT         


        
4条回答
  •  忘了有多久
    2021-02-07 07:11

    Though this question is a little old, but here is my answer:

    This question is of course very platform specific as how standard IO is handled depends on the OS implementation and not on Go language. However, as general rule of thumb (due to some OSes being prevailing), "what you ask is not possible".

    On most of modern operating systems you can pipe standard streams (as in @mraron's answer), you can detach them (this is how daemons work), but you cannot reassign or delegate them to another process.

    I think this limitation is more because of security concern. There are still from time to time bugs being discovered that allow remote code execution, imagine if OS was allowing to reassign/delegate STDIN/OUT, then with such vulnerabilities the consequences would be disastrous.

提交回复
热议问题