从命令行启动进程时如何捕获进程的 PID?
有没有办法纯粹在 .bat 文件中执行此操作?
目的是启动 iexplore.exe,然后在完成后杀死该实例。
Is there a way to do this purely in a .bat file?
The purpose is to launch iexplore.exe
, then kill just that instance when it's finished.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是我使用的:
目录设置为 cmd 文件所在的目录。更改
dir
来更改它。修改cmd
以更改运行哪个命令。如果你想要一个 stop.cmd 来杀死它,它看起来像这样
Here's what I use:
The directory is set to the directory that the cmd file is located in. Change
dir
to alter that. Modifycmd
to change which command is run.If you want a stop.cmd that kills it, it would look like this
您可以使用 vbscript,这是一个创建记事本的示例,然后使用其 pid
保存为 myscript.vbs 并在命令行上终止它
you can use vbscript, here's an example creating notepad, then terminating it using its pid
save as myscript.vbs and on command line
@kybernetikos 提供的答案略有不同,因为它存在解析问题。请注意行
if %%j gr 0 (
A slight variation on the answer provided by @kybernetikos since it has a parsing issue. Note the line
if %%j gr 0 (
大多数情况下,您确实知道要启动什么任务 - 在这种情况下,iexplorer 将显示哪个页面。
那么,
它会杀死显示页面标题的所有实例,但对于特定的标题,最常见的是应该有一个。
汤姆
Most often you do know what task you start - in this case, which page iexplorer shall show.
So how about
It will kill all instances of something showing your page title, but with a specific title most often there should be exactly one.
Tom
嗯,任务列表 & TaskKill?!
Ummm, TaskList & TaskKill?!
由于某种原因,您获取进程 ID 的方法对我来说不起作用,但由于我是批量专家,所以我编写了自己的方法,附在此处:
For some reason your approach of getting process id did not work for me, but since I'm expert in batches, I've coded my own approach, attaching here:
PowerShell 可用于此目的:
PowerShell can be used for this:
我认为你不能用简单的命令行实用程序来做到这一点,因为 IE 实际上会为每个选项卡生成子进程,即如果 IE 尚未运行,你将获得一个父 IE 进程和该选项卡的一个子进程,如果 IE 是已经运行的你只会得到一个子进程。
当你编写自己的工具来杀死 IE 时,这甚至会非常棘手,因为当你杀死一个子(选项卡)进程时,IE 会自动恢复该选项卡。
另请参阅此相关问题:如何获取新创建的 IE8 的进程窗口?(尽管没有好的答案)。
I think you can't do that with simple command line utilities, as IE actually spawns child processes for each tab, i.e. if IE is not yet running you would get one parent IE process and a child process for the tab, and if IE is already running you would simply get a single child process.
It will be even quite tricky when you write your own tool to kill IE because when you kill a child (tab) process, IE will automatically recover this tab.
See also this related question: How to obtain process of newly created IE8 window? (though there is no good answer there).