windowsbatch-filecmd

How to check if a process is running or not in Batch Script


How do I check if a process is alive and running or not using Windows' Batch scripting language?


Solution

  • You can use tasklist to check many facets of tasks running and the OLD school training was give your bat files a title for exactly this reason.

    enter image description here

    First always ensure (and I often forget) use Title in a bat or cmd that is likely to not exit quickly its useful for tasklist as here and on occasion taskkill /t or considered bad if you use /F. Try to use a unique name otherwise editing activate.cmd can show up in notepad and cmd

    @echo off & title ACTIVATED
    echo Running Activated
    pause
    

    You can use other status tests than "running" see tasklist /?

    tasklist /FI "WindowTitle eq ACTIVATED" /FI "STATUS eq running" |  Find /i "cmd"
    REM bad find=2    not found=1   found at least once=0
    if %errorlevel% == 0 (
    echo true) else (
    echo false)
    pause
    

    You may need to use ....ACTIVATED*" if you want to test for multiple entries etc.