英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论