drrvnbwle80177811 2019-07-18 07:23
浏览 198
已采纳

从exec.Command写入文件

I am trying to write a file from a bash command into a file in Go. Note there are several reasons for using Go over bash here: I have some more logic such as parsing configuration files, I would like to run that code for multiple DBs in parallele and finally performing some more complex data manipulation after.

    dumpStr := fmt.Sprintf("pg_dump -U %s -h %s %s | gzip", DbUserName, DbHost, DbName)
    cmd := exec.Command("bash", "-c", dumpStr)
    cmd.Env = append(cmd.Env, "PGPASSWORD="+DbPassword)

    outfile, err := os.Create(DbName + ".gz")
    if err != nil {
        panic(err)
    }
    outfile = cmd.Stdout
    defer outfile.Close()
    err = cmd.Start()
    if err != nil {
        panic(err)
    }
    cmd.Wait()

However, I am getting an emtpy result. I am getting data if I am executing dumpStr from the CLI but not from that code... What am I missing?

  • 写回答

2条回答 默认 最新

  • doumu9799 2019-07-18 07:37
    关注

    As Flimzy said, you're not capturing the output of pg_dump. You can do that with Go, or you can use pg_dump-s --file. It can also compress with --compress so no need to pipe to gzip. Then there's no need for bash and you can avoid shell quoting issues.

    cmd := exec.Command(
        "pg_dump",
        "--compress=9",
        "--file="+DbName + ".gz",
        "-U"+DbUserName,
        "-h"+DbHost,
        DbName,
    )
    
    log.Print("Running pg_dump...")
    
    if err := cmd.Run(); err != nil {
        log.Fatal(err)
    }
    

    Much simpler and more secure.


    For illustration here's how you'd do it all in Go.

    Use Cmd.StdoutPipe to get an open IO reader to pg_dump's stdout. Then use io.Copy to copy from stdout to your open file.

    @Peter points out that since Cmd.Stdout is an io.Reader it's simpler to assign the open file to cmd.Stdout and let cmd write to it directly.

    // Same as above, but no --file.
    cmd := exec.Command(
        "pg_dump",
        "--compress=9",
        "-U"+DbUserName,
        "-h"+DbHost,
        DbName,
    )
    
    // Open the output file
    outfile, err := os.Create(DbName + ".gz")
    if err != nil {
        log.Fatal(err)
    }
    defer outfile.Close()
    
    // Send stdout to the outfile. cmd.Stdout will take any io.Writer.
    cmd.Stdout = outfile
    
    // Start the command
    if err = cmd.Start(); err != nil {
        log.Fatal(err)
    }
    
    log.Print("Waiting for command to finish...")
    
    // Wait for the command to finish.
    if err = cmd.Wait(); err != nil {
        log.Fatal(err)
    }
    

    In addition, you're only checking if the command started, not if it successfully ran.

    From the docs for Cmd.Start.

    Start starts the specified command but does not wait for it to complete.

    The Wait method will return the exit code and release associated resources once the command exits.

    You're checking cmd.Start for an error, but not cmd.Wait. Checking the error from cmd.Start only means the command started. If there is an error while the program is running you won't know what it is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加