Windows 检查一个进程是否正在运行?

回答 4 浏览 3万 2014-04-24

我想检查一个进程是否正在运行? 如果进程没有运行,那么我就执行它(在这个例子中,我把计算器的进程名称=calc.exe)我启动了批处理脚本,但我有一个语法问题,我相信!

我想检查一个进程是否正在运行。

@echo off
Set MyProcess=calc.exe
echo %MyProcess%
pause
for /f "tokens=1" %%i In ('tasklist /NH /FI "imagename eq %MyProcess%"') do set ff=%%i
echo %ff%
If /i %ff%==%MyProcess% (Echo %ff% est en cours d^'execution) Else (Start %MyProcess%)
pause
Hackoo 提问于2014-04-24
你到底在期待什么?除了Info:的输出外,一切都很好,不是吗?Thomas Weller 2014-04-24
如果可执行文件的名称中含有空格或&,你就会遇到问题。foxidrive 2014-04-24
这是一个愚蠢的方式来启动计算。你的问题是出在计算上还是其他程序上?tony bd 2014-04-24
这看起来像是stackoverflow.com/questions/162291/&hellip.com的重复。James Jenkins 2014-04-25
4 个回答
#1楼 已采纳
得票数 5

这里有另一种以你的代码为基础的方法。

@echo off
Set "MyProcess=calc.exe"
echo "%MyProcess%"
tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 (Echo "%MyProcess%" est en cours d^'execution) else (start "" "%MyProcess%")
pause
foxidrive 提问于2014-04-24
#2楼
得票数 4

你可以这样试一试。

tasklist /FI "IMAGENAME eq calc.exe" 2>NUL | find /I /N "calc.exe">NUL
if "%ERRORLEVEL%"=="0" 
echo Running
Rahul Tripathi 提问于2014-04-24
我试了一下,似乎并不奏效:查找。'/I':没有这样的文件或目录 | 查找。'/N':没有这样的文件或目录 | 查找。'hh.exe'。没有这样的文件或目录 | 命令的语法不正确。Fractal 2016-02-18
#3楼
得票数 2

我在Vbscript中的解决方案(°_^)。

Option Explicit
Dim ws,MyApplication,MyProcess
Set ws = CreateObject("WScript.Shell")
MyApplication = "%Programfiles%\WinRAR\WinRAR.exe"
MyProcess = "WinRAR.exe"
Do
'We check if the process is not running so we execute it
    If CheckProcess(MyProcess) = False then 
        Call Executer(DblQuote(MyApplication),0)'0 to Hide the console
'We made ​​a one-minute break and continue in our loop to check 
'whether or not our process exists(in our case = WinRAR.exe)
        Pause(1)
    End if
Loop
'***********************************************************************************************
Function CheckProcess(MyProcess)
    Dim strComputer,objWMIService,colProcessList
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcessList = objWMIService.ExecQuery _
    ("Select Name from Win32_Process WHERE Name LIKE '" & MyProcess & "%'")
        If colProcessList.count > 0 then
            CheckProcess = MyProcess & " is running"
            CheckProcess = True
        else
            CheckProcess = MyProcess & " is not running"
            CheckProcess = False
        End if
    Set objWMIService = Nothing
    Set colProcessList = Nothing
End Function
'***********************************************************************************************
    Sub Pause(NMins)
        Wscript.Sleep(NMins*1000*60)
    End Sub  
'***********************************************************************************************
 Function Executer(StrCmd,Console)
    Dim ws,MyCmd,Resultat
    Set ws = CreateObject("wscript.Shell")
'La valeur 0 pour cacher la console MS-DOS
    If Console = 0 Then
        MyCmd = "CMD /C " & StrCmd & " "
        Resultat = ws.run(MyCmd,Console,True)
        If Resultat = 0 Then
        Else
            MsgBox "Une erreur inconnue est survenue !",16,"Une erreur inconnue est survenue !"
        End If
    End If
'La valeur 1 pour montrer la console MS-DOS
    If Console = 1 Then
        MyCmd = "CMD /K " & StrCmd & " "
        Resultat = ws.run(MyCmd,Console,False)
        If Resultat = 0 Then
        Else
            MsgBox "Une erreur inconnue est survenue !",16,"Une erreur inconnue est survenue !"
        End If
    End If
    Executer = Resultat
End Function
'**********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Hackoo 提问于2014-04-25
Hackoo 修改于2014-04-25
#4楼
得票数 1

好吧,我还找到了另一种方法来实现连续检查(在循环中)进程 "WinRAR.exe "的存在(作为检查应用程序的一个例子),所以我们当然可以改变检查的路径和进程名称。

@echo off
Set "MyApplication=%Programfiles%\WinRAR\WinRAR.exe"
Set "MyProcess=WinRAR.exe"
Color 9B
Title Verification de l^'execution du processus "%MyProcess%" by Hackoo
mode con cols=75 lines=2
:start
cls
tasklist /nh /fi "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 (Echo "%MyProcess%" est en cours d^'execution) else (start "" "%MyApplication%")
ping -n 60 127.0.0.1 >nul 
goto start
Hackoo 提问于2014-04-25