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

后端 未结 18 2077
一个人的身影
一个人的身影 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 07:52

    Building on vtrz's answer and Samuel Renkert's answer on an other topic, I came up with the following script that only runs %EXEC_CMD% if it isn't already running:

    @echo off
    set EXEC_CMD="rsync.exe"
    wmic process where (name=%EXEC_CMD%) get commandline | findstr /i %EXEC_CMD%> NUL
    if errorlevel 1 (
        %EXEC_CMD% ...
    ) else (
        @echo not starting %EXEC_CMD%: already running.
    )
    

    As was said before, this requires administrative privileges.

提交回复
热议问题