一个 Go 二进制文件能够重新加载自身吗?

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

Is it possible for a go binary to reload itself?

问题

我正在使用go-update(https://github.com/inconshreveable/go-update)来更新我分发给用户的Go二进制文件。目前,当运行的Go程序检测到新版本时,它会向用户发送一条消息,要求他们退出并重新启动程序。

运行中的Go程序是否可以从新的二进制文件重新加载自身?

这个问题在go-update的问题跟踪器中提出过,但没有答案:
https://github.com/inconshreveable/go-update/issues/5

英文:

I'm using go-update (https://github.com/inconshreveable/go-update) to update a Go binary that I distribute to users. Right now, when the running go program detects a new version, it sends a message to the user asking them to to quit and restart the program.

Is it possible for the running go program to reload itself from the new binary?

This was asked in the go-update issue tracker, but no answer:
https://github.com/inconshreveable/go-update/issues/5

答案1

得分: 12

是的,可以使用os.Args来获取当前进程的可执行文件名,并使用os.exec包来启动和分叉进程。一个很好的例子是goagain包中的实现,它支持零停机重启。实际上,你可能只需要使用它。

请参考以下链接:
https://github.com/rcrowley/goagain

具体可以查看这个文件:
https://github.com/rcrowley/goagain/blob/master/goagain.go#L77

英文:

Yes, it is possible using os.Args which holds the executable name of the current process, and the os.exec package that can start and fork processes. A good example is how it is done in the goagain package, which supports zero downtime restarts. In fact, you can probably just use it.

See https://github.com/rcrowley/goagain

and more specifically in this file: https://github.com/rcrowley/goagain/blob/master/goagain.go#L77

huangapple
  • 本文由 发表于 2015年1月7日 23:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/27823042.html
匿名

发表评论

匿名网友

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

确定