Search code examples
windowsbatch-filetaskkill

Safely kill a process from .bat file


I have a VB6 application that needs to update it's self. For this purpose, the PM has recommended using a batch file that is to be launched from the application. The batch file should kill the process, download the new version from a local server, overwrite the old files and launch the application again.
My problem with this is that I am not sure that taskkill returns control the the parent only after the process has been killed and all the used resources have been released; in particular, we were wondering what happens to the process' .exe file. Is there any guarantee that it will be unlocked after taskkill returns? - of course, theoretically it shouldn't be locked by any other process, this is the only case we're interested in.


Solution

  • If the process is killed, then the exe will be freed up for the update. As an alternative to using taskkill, you can use pskill from sysinternals, but taskkill should work great with the /IM parameter.

    However, neither pskill or taskkill will not wait for the process to stop before returning control. So you would have to monitor the processes to make sure they're no longer running.

    Also, and this would be my preferred solution, it's possible to deploy a vb6 app via click-once, which will automatically update the app with new releases. Check this article for details.

    As for the resources opened by the process, it's possible that they might remain open (such as a sql connection), but most likely they will be successfully closed. You'll only be able to know for sure by monitoring what happens by testing out taskkill on the process.

    Hope that helps.