dsvbtgo639708 2018-06-21 07:40 采纳率: 0%
浏览 66
已采纳

使用错误代码调用另一个程序后,Go程序结束

I have the following problem: I'm calling "cryptsetup" out of a go routine. Therefore, if the cryptsetup succeeds and exits with exitcode 0, everything is ok. If cryptsetup runs into an error, like a wrong password, and exits with an error code != 0, my go function only prints the error and then exits without a panic or an errorcode.

openCmd := exec.Command("cryptsetup", "luksOpen", *container, "container")
var inPipe io.WriteCloser
if inPipe, err = openCmd.StdinPipe(); err == nil {
    if err = openCmd.Start(); err == nil {
        log.Println("cryptsetup command started!")
        inPipe.Write([]byte(pwd))
        log.Println("luks password passed!")
        inPipe.Close()
        log.Println("stdin pipe closed!")
        if err = openCmd.Wait(); err == nil {
            log.Println("Container opened!")
            if err = exec.Command("mount", "-t", "ext4", "/dev/mapper/container", "/mnt").Run(); err == nil {
                mountStatus.isMounted = true
                pwd = ""
                log.Println("Container mounted!")
                return true
            } else {
                log.Fatalf("Couldn't mount partition: %s", err)
            }
        } else {
            log.Fatalf("cryptsetup execution failed: %s", err)
        }
    } else {
        log.Fatalf("Couldn't start cryptsetup command: %s", err)
    }
} else {
    log.Fatalf("Couldn't open cryptsetup stdin: %s", err)
}

My console output looks like this:

2018/06/21 09:05:17 cryptsetup command started!
2018/06/21 09:05:17 luks password passed!
2018/06/21 09:05:17 stdin pipe closed!
2018/06/21 09:05:20 cryptsetup execution failed: exit status 2

I have no idea why my program exits. Can anyone help?

  • 写回答

1条回答 默认 最新

  • douzong5057 2018-06-21 07:50
    关注

    Because you are calling log.Fatalf(), and quoting from its doc:

    Fatalf is equivalent to Printf() followed by a call to os.Exit(1).

    log.Fatalf() terminates your app on purpose. If you don't want it to terminate, don't call log.Fatalf(), but e.g. log.Printf() instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成