删除自己的二进制文件的可能性

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

Possibilities for deleting own binary

问题

这里的背景是,我在golang中创建了一个安装程序(跨平台,但最初针对Windows)。

我必须运行一个卸载过程。这个过程的最后一步是删除我的应用程序二进制文件和另一个运行时文件。

我目前的方法是特定于Windows的,而且相当简陋-无法确认进程是否已完成-基本上只是从命令行调用删除命令,使用ping创建延迟,类似于这样:

cmd := exec.Command("ping 127.0.0.1", "-n", "5", ">", "nul", "&&", "del", os.Args[0])
cmd.Start()
os.Exit(0)

就像我说的,这个方法可以工作,但我希望能够在退出之前确认进程是否已完成。我想象中的解决方案是启动一种可以在应用程序生命周期之外持久存在的内存中的goroutine,而不会锁定文件。-这种可能吗?

注意-我还考虑过创建第二个golang应用程序(或者这个应用程序的副本),从临时目录中执行此删除操作(这至少可以让我实现跨平台),然后让操作系统删除该副本,但根据我所读到的,Windows在清除临时目录方面并不特别及时,所以我不确定这是否是理想的解决方案。我希望安装/卸载周期使机器保持与初始状态相同。

还有其他建议吗?跨平台解决方案将是一个额外的优势。

更新
我提出的ping和删除解决方案实际上并没有像我希望的那样工作(至少对于系统文件夹中的文件而言)。我通过创建第二个golang应用程序来解决这个问题,将其生成到临时文件夹中,然后在用户重新启动时创建一个run_once注册表项来删除该文件。

我对这个解决方案仍然不满意,感觉有点笨拙。

我已经为Adrian的澄清提供了赞赏,但我欢迎任何关于替代方法的建议。

英文:

The background here is that I have created an installer in golang (cross-platform but initially targeting windows)

I have to run an uninstall process. The last step of this process is to delete my apps binary and another runtime file.

My current approach is windows specific and pretty poor- impossible to get confirmation the process has completed- basically im just calling delete from the command line, using ping to create a delay, something like this:

cmd := exec.Command("ping 127.0.0.1", "-n", "5", ">", "nul", "&&", "del", os.Args[0])
cmd.Start()
os.Exit(0)

As I say this works but I would prefer to be able to confirm the process has completed before exiting. My imagined solution would be to launch some kind of in-memory goroutine that can persist beyond the application lifetime without locking the file. - Is this possible?

Note - I have also considered creating a second golang app (or a copy of this one) which carries out this deletion from a temp directory (that would at least give me cross-platform) and then let the OS remove that copy but from what I've read windows is not particularly prompt at clearing temp directories so I'm not sure this is ideal either. I would prefer that an install/uninstall cycle leave the machine in the same state it found it

Any other suggestions as to how I may achieve this? Cross platform solution would be a bonus.

Update
The ping and delete solution I suggested did not in fact work as I hoped (at least not for files within system folders). I solved this by creating a second golang app to run the deletion, I spawn this into a temp folder and then create a run_once entry in the registry to delete that file when the user reboots.

I am still not happy with this solution, it feels rather hacky.

I have upvoted Adrian for his clarification but would welcome any suggestions for alternative approaches.

答案1

得分: 3

不,应用程序的生命周期是完全二进制的,要么正在运行,要么没有运行。它不能退出但仍然继续运行。但是,你可以通过工程方法,在删除二进制文件之前,优雅地停止你担心的应用程序的某些部分。具体的实现方式完全取决于你的应用程序,但这是可行的。例如,如果它是一个Web服务器,你可以优雅地停止http.Server,然后当它完成后再删除二进制文件。

英文:

>launch some kind of in-memory goroutine that can persist beyond the application lifetime without locking the file. - Is this possible?

No. The application lifetime is completely binary - either it's running or it isn't. It can't exit yet keep running. You can, through engineering, gracefully stop whatever parts of the application you're worried about before deleting the binary, however. The implementation would be completely dependent on your application but it's certainly feasible. For example, if it's a web server, you could gracefully stop the http.Server, then when that finishes, delete the binary.

huangapple
  • 本文由 发表于 2017年8月22日 20:41:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/45817962.html
匿名

发表评论

匿名网友

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

确定