0

Short version of the question here,

I run an exceutable (which in turn runs a simulation) from a batch file. I can run up to four of these at once. Is there any method for checking (from the batch file) how many of these executables are running at a time?

Long version,

I have a batch file which allows me to run multiple models but dues to licencing I cannot run more than 4 models simultaneously. I can however queue them up to be run, however i am struggling to write a batch file which ensures that there are always four models running at all times. My best effort (cobbled together from searching the internet) is below

            FOR %%a in (%A%) do (
                FOR %%b in (%B%) DO (
                    FOR %%c in (%C%) DO (
                         FOR %%d in (%D%) DO (
                            SET /A COUNT+=1
                            SET /A Result =!COUNT!%%4
                            :: set Result to the remainder of the Count divided by 4
                            if !Result! == 0 (
                                start "TUFLOW" /wait C:\Tuflow\TuflowExe\Build_2011_09_AF_64\TUFLOW_iSP_w64.exe -e1 %%a -s1 %%b -s2 %%c -s3 %%d C:\Tuflow\Model\Ohoka\runs\OH.tcf
                            ) else (
                                start "TUFLOW" C:\Tuflow\TuflowExe\Build_2011_09_AF_64\TUFLOW_iSP_w64.exe -e1 %%a -s1 %%b -s2 %%c -s3 %%d C:\Tuflow\Model\Ohoka\runs\OH.tcf
                            )
                         )
                    )
                )
            )

Where A, B, C and D are arrays containing variable combinations that are passed to the executable. I then have a count which records the iterations, on every fourth iteration the "True" condition for the if statement is triggered and the batch waits until the model run is completed before continuing.

This is ok, but my other three models may finish earlier, freeing up valuable cpu time that is not utilised because the batch is waiting for the fourth iteration to complete. Or worse, the fourth iteration completes prior to the other three and ends up trying to run more than four models which causes a licensing pop up which requires a user to dismiss.

Any suggestions or other solutions that I am bound to have missed?

user1650538
  • 228
  • 1
  • 9

2 Answers2

2

It's relatively easy to count the number of programs running by using tasklist.exe

for /f "skip=3" %%x in ('tasklist /FI "IMAGENAME eq TUFLOW_iSP_w64.exe"') do set /a count=count+1
echo TUFLOW_iSP_w64.exe is running %count% times.

Just remember to clear count or set it to 0 each time you run it.

Or, more slowly:

for /f %%x in ('tasklist ^| find /c "TUFLOW_iSP_w64.exe"') set count=%%x
Echo TUFLOW_iSP_w64.exe is running %count% times.

The above code is slower because find has to process the entire tasklist.

James K
  • 4,005
  • 4
  • 20
  • 28
1

I'd try a polling loop that runs every few seconds, calls

tasklist | findstr "TUFLOW"

gets the number of lines in the output, and starts a new run if the number is less than four.

You don't seem to have any problem writing complex batch files, so I will not try the coding here. Counting the lines of the command output can be done in a FOR /F. The polling delay will take a sleep implementation. If you have e.g. Cygwin installed, you'll already have this. Most Windows versions have an undocumented command timeout NumberOfSeconds, which I learned about in this article .

Community
  • 1
  • 1
Gene
  • 46,253
  • 4
  • 58
  • 96
  • You can always use `choice.exe` as a timeout `choice /n /d y /t 5 > nul` will wait for 5 seconds (unless you press `y` or `n`). – James K Sep 06 '12 at 02:34
  • Thanks. These incredibly arcane ways of getting stuff done are a reason to use Perl or Powershell or almost any other scripting solution in lieu of cmd.exe. – Gene Sep 06 '12 at 03:01
  • Yeah, but nothing feels quite as satisfying as kicking the butt of an 'impossible' problem in batch. :) – James K Sep 06 '12 at 04:41