从命令行启动进程时如何捕获进程的PID?

时间:2009-11-27 09:59:28

标签: process cmd pid

有没有办法纯粹在.bat文件中执行此操作?

目的是启动iexplore.exe,然后在该实例完成时终止它。

8 个答案:

答案 0 :(得分:10)

以下是我使用的内容:

@echo off

rem there is a tab in the file at the end of the line below
set tab=    
set cmd=javaw -jar lib\MyProg.jar
set dir=%~dp0

echo Starting MyProg
set pid=notfound

for /F "usebackq tokens=1,2 delims=;=%tab% " %%i in (
    `wmic process call create "%cmd%"^, "%dir%"`
) do (
    if /I %%i EQU ProcessId (
        set pid=%%j
    )
)

echo %pid% > MyProg.pid

目录设置为cmd文件所在的目录。更改dir以更改该目录。修改cmd以更改运行的命令。

如果你想要一个杀死它的stop.cmd,它看起来像这个

@echo off
for /F %%i in (%~dsp0MyProg.pid) do taskkill /F /PID %%i
del %~dsp0MyProg.pid

答案 1 :(得分:3)

你可以使用vbscript,这是创建记事本的示例,然后使用其pid终止它

strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("notepad.exe", null, objConfig, PID)
If errReturn = 0 Then
    WScript.Echo "Process ID is: " & PID
End If 

WScript.Echo "Ready to kill process: " & PID & "? [Y|y]"
Do While Not WScript.StdIn.AtEndOfLine
   strInput = strInput & WScript.StdIn.Read(1)
Loop
If LCase(strInput) = "y" Then
    WScript.Echo "Select * from Win32_Process Where ProcessId = '" & PID & "'"
    Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where ProcessId = '" & PID & "'")
    For Each objProcess in colProcessList
        objProcess.Terminate()
    Next
End If 

另存为myscript.vbs和命令行

c:\test> cscript /nologo myscript.vbs

答案 2 :(得分:2)

嗯,TaskList& TaskKill?!

答案 3 :(得分:2)

@kybernetikos提供的答案略有不同,因为它有解析问题。请注意第if %%j gr 0 (

@echo off

rem there is a tab in the file at the end of the line below
set tab=    
set cmd=javaw -jar lib\MyProg.jar
set dir=%~dp0

echo Starting MyProg
set pid=notfound

for /F "usebackq tokens=1,2 delims=;=%tab% " %%i in (
    `wmic process call create "%cmd%"^, "%dir%"`
) do (
    if %%j gtr 0 (
        set pid=%%j
    )
)

echo %pid% > MyProg.pid

答案 4 :(得分:2)

大多数情况下,你确实知道你开始的任务 - 在这种情况下,iexplorer将显示哪个页面。

那怎么样?

taskkill /FI "Windowtitle eq *yourpagetitle*"

它会杀死显示您网页标题的所有内容,但最常使用的是具有特定标题的内容。

汤姆

答案 5 :(得分:1)

出于某种原因,你获取进程ID的方法对我来说不起作用,但由于我是批处理专家,我编写了自己的方法,附在此处:

@echo off

call:AsyncCmd
rem call:AsyncCmd "echo hello world"
rem call:AsyncCmd "call build.bat"
exit /b

rem ------------------------------------------------------------------------------------------
rem Starts asynchronous command execution 
rem %1 is command, if empty - only aborts existing build.
rem ------------------------------------------------------------------------------------------
:AsyncCmd
if exist %~dp0SetupBuild_Completed.txt (
    del /f %~dp0SetupBuild_Pid.txt >nul 2>&1
    del /f %~dp0SetupBuild_Completed.txt >nul 2>&1
)

if not exist %~dp0SetupBuild_Pid.txt goto lStartProc
    rem --------------------------------------------------
    rem Abort build process
    rem --------------------------------------------------
    set /p pid=<%~dp0SetupBuild_Pid.txt
    echo Cancelling setup build process, process id %pid%
    pskill -t %pid%
    del /f %~dp0SetupBuild_Pid.txt >nul 2>&1

:lStartProc
if "%~1" == "" exit /b 0

rem --------------------------------------------------
rem Starts asyncronous build process
rem --------------------------------------------------
set dir=%~dp0
set dir=%dir:~0,-1%
for /f "tokens=2 delims==; " %%a in ('wmic process call create "cmd /c mincon.exe && %~1 && echo 1>%~dp0SetupBuild_Completed.txt"^, "%dir%" ^| findstr /r "ProcessId"') do set pid=%%a
echo Setup build started, process id: %pid%
echo %pid%>%~dp0SetupBuild_Pid.txt
exit /b 0

答案 6 :(得分:0)

我认为你不能用简单的命令行实用程序来做到这一点,因为IE实际上会为每个选项卡生成子进程,即如果IE尚未运行,你将获得一个父进程和进程的子进程,以及如果IE已经运行,你只需要一个子进程。

当您编写自己的工具来杀死IE时会非常棘手,因为当您杀死子(Tab)进程时,IE将自动恢复此选项卡。

另见相关问题:How to obtain process of newly created IE8 window?(虽然没有好的答案)。

答案 7 :(得分:0)

PowerShell可以用于此:

powershell -executionPolicy bypass -command "& {$process = start-process $args[0] -passthru -argumentlist $args[1..($args.length-1)]; exit $process.id}" notepad test.txt

echo Process ID of new process: %errorlevel%