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

后端 未结 18 2276
一个人的身影
一个人的身影 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条回答
  •  Happy的楠姐
    2020-11-22 08:04

    The answer provided by Matt Lacey works for Windows XP. However, in Windows Server 2003 the line

     tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log
    

    returns

    INFO: No tasks are running which match the specified criteria.

    which is then read as the process is running.

    I don't have a heap of batch scripting experience, so my soulution is to then search for the process name in the search.log file and pump the results into another file and search that for any output.

    tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log
    
    FINDSTR notepad.exe search.log > found.log
    
    FOR /F %%A IN (found.log) DO IF %%~zA EQU 0 GOTO end
    
    start notepad.exe
    
    :end
    
    del search.log
    del found.log
    

    I hope this helps someone else.

提交回复
热议问题