关闭外部应用程序

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

Go - close an external application

问题

我正在使用OSX机器上的Go,并尝试编写一个程序来打开一个外部应用程序,然后在几秒钟后关闭它 - 关闭应用程序,而不是退出Go脚本。

我正在使用https://github.com/skratchdot/open-golang上可用的库来启动应用程序,它运行良好。我已经设置了超时。但是当我需要关闭应用程序时,问题就出现了。

有人能给我一个提示,我如何退出应用程序吗?

提前感谢。

英文:

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.

答案1

得分: 2

看起来这个库隐藏了你用来关闭程序的细节,特别是进程ID(PID)。

如果你改用os/exec包启动程序或获取该PID的句柄,你可以使用Process对象来终止或发送信号给应用程序,尝试优雅地关闭它。

https://golang.org/pkg/os/#Process

英文:

It looks like that library is hiding details that you'd use to close the program, specifically the process ID (PID).

If you launch instead with the os/exec package or get a handle on that PID then you can use the Process object to kill or send signals to the app to try and close it gracefully.

https://golang.org/pkg/os/#Process

答案2

得分: 1

谢谢大家的帮助。我能够通过以下代码实现我想要的功能。

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): // 在30秒后终止进程
    if err := cmd.Process.Kill(); err != nil {
        log.Fatal("failed to kill: ", err)
    }
    <-done // 允许goroutine退出
    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() // 时间流逝...
err = cmd.Wait()

我将这段代码放在使用os/exec包启动应用程序之后,就像@JimB建议的那样。

英文:

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

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

huangapple
  • 本文由 发表于 2015年6月12日 21:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/30803877.html
匿名

发表评论

匿名网友

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

确定