douzhi1924 2017-08-22 12:41
浏览 65

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

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条回答 默认 最新

  • duanjiwei1283 2017-08-22 12:46
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算