Script to check process running and finalizing or not

1

I have a process called monitor.exe and it can not have 2 active processes because it is a print monitor that if it has 2 active processes it will issue 2 duplicate invoices, so I would like to know if it has how do I make a script that checks if the process is active, if it has only 1 active and if it has more than 1 it finishes and leaves only 1 active?

I tried to make a simple script with taskkill / F / IM monitor.exe and then run a start \ path .exe \

But sometimes when I finish the process it goes up alone so if I give the start the 2 duplicates again, does anyone have any ideas?

    
asked by anonymous 19.04.2018 / 15:01

1 answer

0

The wmic process where name="monitor.exe" | find "monitor.exe" /c command returns the number of processes in the monitor.exe file running.

In order to check the number of running processes, we can save the result of this command to the %pcount% variable with the script below:

@echo off

set pname=monitor.exe

set pcommand="wmic process where name="%pname%" | find "%pname%" /c"

FOR /F "tokens=*" %%i IN (' %pcommand% ') DO SET pcount=%%i

if %pcount% GTR 1 (
    echo Tem mais de um processo.
    taskkill /f /im %pname%
    start pasta\para\%pname%

) else (
    echo Tem um ou nenhum processo.
)
pause
    
19.04.2018 / 21:16