netdigger
netdigger

Reputation: 3789

Keep X instances of Y running at all time, windows

How can I, with a batch script make sure that I always run X instances of for example cmd.exe?

I've search and found that this gives me nbr X lines if X instances is running..

tasklist /FI "IMAGENAME eq exe.exe" 2>NUL | find /I /N "exe.exe"

And then do START XXX X times if one has stopped running...?

Upvotes: 2

Views: 98

Answers (1)

Alex K.
Alex K.

Reputation: 175826

How about

set PROG=exe.exe
set COUNT=0
set WANT=5
for /f "skip=1" %%i IN ('wmic process where Name^="%PROG%" get Name') do (
     if /i %%i equ %PROG% set /a COUNT+=1
)
echo count: %COUNT%
set /a WANT-=COUNT
if %WANT% gtr 0 for /l %%i in (1,1,%WANT%) do start %PROG%

Upvotes: 1

Related Questions