windows 从 .bat 文件安全地终止进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/203935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:28:00  来源:igfitidea点击:

Safely kill a process from .bat file

windowsbatch-filetaskkill

提问by Tom

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.

我有一个需要自我更新的 VB6 应用程序。为此,PM 建议使用要从应用程序启动的批处理文件。批处理文件应该终止进程,从本地服务器下载新版本,覆盖旧文件并再次启动应用程序。
我的问题是我不确定 taskkill 只有在进程被终止并且所有使用的资源都被释放后才返回控制父进程;特别是,我们想知道进程的 .exe 文件会发生什么。有没有保证taskkill回来后会解锁?- 当然,理论上它不应该被任何其他进程锁定,这是我们唯一感兴趣的情况。

采纳答案by Jeff Schumacher

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.

如果该进程被终止,则 exe 将被释放以进行更新。作为使用 taskkill 的替代方法,您可以使用 sysinternals 中的 pskill,但 taskkill 应该与 /IM 参数配合使用。

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.

但是,无论是 pskill 还是 taskkill 都不会在返回控制权之前等待进程停止。因此,您必须监视进程以确保它们不再运行。

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 articlefor details.

此外,这将是我的首选解决方案,可以通过单击一次部署 vb6 应用程序,这将使用新版本自动更新应用程序。查看这篇文章了解详情。

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.

至于进程打开的资源,它们有可能保持打开状态(例如sql连接),但很可能会成功关闭。您只能通过测试过程中的 taskkill 来监控发生的情况,从而确定地知道。

Hope that helps.

希望有帮助。

回答by GvS

I do this, by letting the app itself check for updates. If there are, then the app downloads them in a temporary place. After that it will start a (.js) script and exits the app. This script will wait for the process to close (sleep), copy the updates and start the app again.

我通过让应用程序本身检查更新来做到这一点。如果有,则应用程序会将它们下载到临时位置。之后它将启动一个 (.js) 脚本并退出应用程序。此脚本将等待进程关闭(休眠),复制更新并再次启动应用程序。

This way you have far less logic in the script, and you can do your stuff in an environment you have more tools available.

通过这种方式,您在脚本中的逻辑要少得多,而且您可以在拥有更多可用工具的环境中完成您的工作。

回答by Gautam Jain

Have a command line parameter like /Shutdown that the VB program will process.

有一个命令行参数,如 VB 程序将处理的 /Shutdown。

MyApp.exe /Shutdown

MyApp.exe /关机

/Shutdown parameter will find existing running instance of the app (using FindWindow API) and send a user message to the window requesting it to shutdown smoothly.

/Shutdown 参数将查找应用程序的现有运行实例(使用 FindWindow API)并向窗口发送用户消息,请求其平稳关闭。

回答by rslite

Taskkill should return only after the process has been terminated (as per comments below: use the /F flag to force termination of the program if it expects user input).

Taskkill 应仅在进程终止后返回(根据以下注释:如果需要用户输入,请使用 /F 标志强制终止程序)。

To learn more about it you can use Process explorerand see exactly when the process is terminated and also if anyone still has the .exe file open (Ctrl+F and the name of the exe) - but that shouldn't be the case.

要了解有关它的更多信息,您可以使用Process explorer并准确查看进程何时终止,以及是否有人仍然打开 .exe 文件(Ctrl+F 和 exe 的名称) - 但事实并非如此。