dongzhina7098 2015-06-12 13:06
浏览 147
已采纳

继续-关闭外部应用程序

I'm using Go on an OSX machine and trying to make a program to open an external application and then after few seconds, close it - the application, not exit the Go script.

I'm using the library available on https://github.com/skratchdot/open-golang to start the app and it works fine. I also already have the timeout running. But the problem comes when I have to close the application.

Would someone give a hint of how I would be able to exit the app?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dronthpi05943 2015-06-12 15:03
    关注

    Thank you guys for the help. I would able to do what I was trying with the following code.

    cmd := exec.Command(path string)
        err := cmd.Start()
      if err != nil {
        log.Printf("Command finished with error: %v", err)
      }
      done := make(chan error, 1)
      go func() {
        done <- cmd.Wait()
      }()
      select {
      case <-time.After(30 * time.Second):      // Kills the process after 30 seconds
        if err := cmd.Process.Kill(); err != nil {
          log.Fatal("failed to kill: ", err)
        }
        <-done // allow goroutine to exit
        log.Println("process killed")
        indexInit()
        case err := <-done:
          if err!=nil{
            log.Printf("process done with error = %v", err)
          }
      }
        if err != nil {
            log.Fatal(err)
        }
        log.Printf("Waiting for command to finish...")
      //timer() // The time goes by...
        err = cmd.Wait()
    }
    

    I placed that right after start the app with the os/exec package as @JimB recommended.

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

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭