Reputation: 6079
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.
Upvotes: 2
Views: 435
Reputation: 46532
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.
Upvotes: 3