检查进程是否正在运行?

4

我想要检查进程是否在运行。 如果进程没有在运行,那么我会执行它(在这个例子中,我选了一个名为 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

你到底期望什么?除了“Info:”输出之外,一切都正常工作,不是吗? - Thomas Weller
您将会遇到具有空格或&符号的可执行文件命名问题。 - foxidrive
这是一个愚蠢的启动计算器的方式。你的问题是与计算器还是其他程序有关? - tony bd
所以,我想感谢大家的回复和解决方案,最终,我采用了foxidrive的解决方案,对我来说非常有效,这正是我期望的结果。 - Hackoo
2
可能是重复的问题:如何通过批处理脚本检查进程是否正在运行 - James Jenkins
2
这看起来像是 https://dev59.com/0HVC5IYBdhLWcg3w21Eq 的重复。 - James Jenkins
5个回答

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

4
你可以尝试这样做:

你可以尝试以下方法:

tasklist /FI "IMAGENAME eq calc.exe" 2>NUL | find /I /N "calc.exe">NUL
if "%ERRORLEVEL%"=="0" 
echo Running

我尝试了这个,但似乎没有起作用:find: '/I': 没有那个文件或目录 | find: '/N': 没有那个文件或目录 | find: 'hh.exe': 没有那个文件或目录 | 命令的语法不正确。 - Fractal

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
'**********************************************************************************************

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

0
最近我做了类似的事情,当我想要打开一个应用程序时,只有在该进程尚不存在的情况下才会执行。
TASKLIST | FINDSTR myProgram
if %ERRORLEVEL% neq 0 ("your conditions here")

你可以检查一个程序是否在任务列表中。在任务列表上使用findstr将返回一个错误级别为0,如果它在任务列表中的话。你可以利用这个来构建出在检查之后的条件。
TASKLIST | FINDSTR git-bash || START "" "C:\Program Files\Git\git-bash.exe"

上面是我一行代码的解决方案,用于打开git-bash,如果还没有一个实例在运行的话。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接