How to check if a process is running via a batch script

后端 未结 18 2069
一个人的身影
一个人的身影 2020-11-22 07:38

How can I check if an application is running from a batch (well cmd) file?

I need to not launch another instance if a program is already running. (I can\'t change th

相关标签:
18条回答
  • 2020-11-22 08:07

    TrueY's answer seemed the most elegant solution, however, I had to do some messing around because I didn't understand what exactly was going on. Let me clear things up to hopefully save some time for the next person.

    TrueY's modified Answer:

    ::Change the name of notepad.exe to the process .exe that you're trying to track
    ::Process names are CASE SENSITIVE, so notepad.exe works but Notepad.exe does NOT
    ::Do not change IMAGENAME
    ::You can Copy and Paste this into an empty batch file and change the name of
    ::notepad.exe to the process you'd like to track
    ::Also, some large programs take a while to no longer show as not running, so
    ::give this batch a few seconds timer to avoid a false result!!
    
    @echo off
    SETLOCAL EnableExtensions
    
    set EXE=notepad.exe
    
    FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound
    
    goto ProcessNotFound
    
    :ProcessFound
    
    echo %EXE% is running
    goto END
    :ProcessNotFound
    echo %EXE% is not running
    goto END
    :END
    echo Finished!
    

    Anyway, I hope that helps. I know sometimes reading batch/command-line can be kind of confusing sometimes if you're kind of a newbie, like me.

    0 讨论(0)
  • 2020-11-22 08:08

    The suggestion of npocmaka to use QPROCESS instead of TASKLIST is great but, its answer is so big and complex that I feel obligated to post a quite simplified version of it which, I guess, will solve the problem of most non-advanced users:

    QPROCESS "myprocess.exe">NUL
    IF %ERRORLEVEL% EQU 0 ECHO "Process running"
    

    The code above was tested in Windows 7, with a user with administrator rigths.

    0 讨论(0)
  • 2020-11-22 08:08

    I don't know how to do so with built in CMD but if you have grep you can try the following:

    tasklist /FI "IMAGENAME eq myApp.exe" | grep myApp.exe
    if ERRORLEVEL 1 echo "myApp is not running"
    
    0 讨论(0)
  • 2020-11-22 08:08

    Just mentioning, if your task name is really long then it won't appear in its entirety in the tasklist result, so it might be safer (other than localization) to check for the opposite.

    Variation of this answer:

    :: in case your task name is really long, check for the 'opposite' and find  the message when it's not there
    tasklist /fi "imagename eq yourreallylongtasknamethatwontfitinthelist.exe" 2>NUL | find /I /N "no tasks are running">NUL
    if "%errorlevel%"=="0" (
        echo Task Found
    ) else (
        echo Not Found Task
    )
    
    0 讨论(0)
  • 2020-11-22 08:12

    You should check the parent process name, see The Code Project article about a .NET based solution**.

    A non-programmatic way to check:

    1. Launch Cmd.exe
    2. Launch an application (for instance, c:\windows\notepad.exe)
    3. Check properties of the Notepad.exe process in Process Explorer
    4. Check for parent process (This shows cmd.exe)

    The same can be checked by getting the parent process name.

    0 讨论(0)
  • 2020-11-22 08:14

    I'm assuming windows here. So, you'll need to use WMI to get that information. Check out The Scripting Guy's archives for a lot of examples on how to use WMI from a script.

    0 讨论(0)
提交回复
热议问题