Batch scripts queueing loop script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Batch scripts queueing loop script

#1 Post by KgOlve » 27 Feb 2013 04:29

Hi,
I'm wondering if it's possible to make a batch script detect if and how many of a process instances are already running, and if too many are already running, make the upcoming instances of the process timeout and try again with time intervals untill few enough of the instances are running to let the next one run.

So for instance, i want to run 3 identical batch scripts, which each runs a process which takes about 10 minutes.
First starts, and starts the process.
About 1 minute later the second batch script starts, and starts another instance of the process.
Another 1 minute later, the third instance starts, but what i want it to do is check if there aren't already 2 instances of the process currently going, and if there are, i want it to timeout for a few minutes and then loop back and try again, again and again, untill one of the first 2 processes finishes, and the third batch script can run it's process and then finish up.

So something like

:process-check
if process.exe >2 already running{
do timeout 90 (seconds)
goto:process-check
}

if process.exe <2 already running {
goto:run-process
}

I hope i made what i want understandable.
I appreciate any help :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch scripts queueing loop script

#2 Post by foxidrive » 27 Feb 2013 04:45

This should work, if process.exe is unique. (untested)

EDITED: :loop was omitted - rectified. Changed 4 to 2 processes.

Code: Select all

@echo off
:loop
for /f %%a in ('tasklist ^|find /i "process.exe" ^|find /c /v "" ') do (
if %%a LSS 2 start "" "process.exe"
)
ping -n 60 localhost >nul
goto :loop

KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Re: Batch scripts queueing loop script

#3 Post by KgOlve » 27 Feb 2013 06:30

Yes, process.exe is unique, but there can be run many simultaneous instances of it (many process.exe at the same time, with the same process name), which is what i'm trying to queue with this script, since it takes alot of processing power, i'd like it to only be able to run 2 instances of the process at the same time, no matter how many times i run the batch script that initializes it.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch scripts queueing loop script

#4 Post by Ed Dyreen » 27 Feb 2013 07:09

'
dbenham posted a way that allows to start child processes and have the parent detect their state. This technique utilizes the fact that only one process can open a file for writing simultaneously.

Another technique which I still use is childs writing keys to the registry eg:
key : 'isrunning.processName' val: 'numberOfProcesses'

The 2nd less efficient but more independent. In both cases a locking mechanism is critical.

Regards,
ed

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch scripts queueing loop script

#5 Post by foxidrive » 27 Feb 2013 08:44

KgOlve wrote:i'd like it to only be able to run 2 instances of the process at the same time, no matter how many times i run the batch script that initializes it.


Did you try the script I wrote? It should limit it to 2 instances if you change the 4 to 2.

KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Re: Batch scripts queueing loop script

#6 Post by KgOlve » 27 Feb 2013 08:47

The script i already have is used for processing files and give an output file.
Files are dropped onto the .bat file.
It then sets the filename, checks if the output file for it already exists, and if the file doesn't already exist it then moves on to start the process which will process the file and create the output file. The script waits for the process to finish and then it moves onto exiting the batch window.
I want the script to stall inbetween the <<checking if the output already exists>> and the <<start process>> stadiums, all in the one and same script, and continue checking if there are few enough processes of the same kind already running for it to be allowed to continue onto starting it's own process.
Sometimes well over 10 of this batch script starts and start the process overloading the computer (It's semi-automatic). So if another 10 child processes were to pop up, there'd be 20 windows all of a sudden instead of 10.

So starting lots of child processes would be bad. And it's a shared computer, so i don't want to fickle with registry values either.

Isn't there any way to be able to do that?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch scripts queueing loop script

#7 Post by foxidrive » 27 Feb 2013 08:55

Yes. See my edited post with the code above.


You might try saying 'Thank you' next time when someone offers free help, and if the code doesn't work then you can say so.

KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Re: Batch scripts queueing loop script

#8 Post by KgOlve » 27 Feb 2013 09:11

foxidrive wrote:Yes. See my edited post with the code above.


You might try saying 'Thank you' next time when someone offers free help, and if the code doesn't work then you can say so.


I haven't gotten around to trying to code yet, but thank you to both you and Ed Dyreen who have offered help :)

Edit to avoid doubleposting:
I changed your script some to fit my already existing script, could you tell me if this is correct?

Code: Select all

:queue
for /f %%a in ('tasklist ^|find /i "process.exe" ^|find /c /v "" ') do (
if not %%a LSS 2 goto:queue
)

I also omitted the ping part, i'm not sure i understand what that part was supposed to do.

Queue
Posts: 31
Joined: 16 Feb 2013 14:31

Re: Batch scripts queueing loop script

#9 Post by Queue » 27 Feb 2013 14:02

The ping adds a delay between loops. It's not actually pinging anything meaningful, just your own computer (localhost) 60 times. You really don't want tasklist (and then 2 finds, and the supporting cmd.exe instances to support the piping) pounding away in batch in a constant loop without some sort of sleep.

Looking at what you edited, ''if not %%a LSS 2'' is rather silly; it's the same as ''if %%a GEQ 2''. If you just want the stall, and you'll handle all the supporting goto logic:

Code: Select all

:stall
for /f %%a in ('tasklist ^|find /i "process.exe" ^|find /c /v "" ') do if %%a GEQ 2 (
ping -n 60 localhost >nul
goto:stall
)

Queue

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch scripts queueing loop script

#10 Post by foxidrive » 27 Feb 2013 14:14

You can use this variation where it will only fall through to the code below it when the processes are below 2 instances. You will need to branch back to the :loop yourself.

As Queue quite rightly says, the ping acts as a low CPU usage 60 second sleep command to stop the loop running continually.

Code: Select all

@echo off
:loop
ping -n 60 localhost >nul
for /f %%a in ('tasklist ^|find /i "process.exe" ^|find /c /v "" ') do if %%a GTR 1 goto :loop

:: your code goes here


KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Re: Batch scripts queueing loop script

#11 Post by KgOlve » 27 Feb 2013 14:32

I always used the timeout command for pauses. i.e. "timeout 60" for a 60 second timeout, so i never knew about the ping command. The timeout command even counts down in the commandline window.

Thanks for the help. I will try inputting the codes into the script now, and see how it goes. I was hurriedly making responses earlier since i've been very busy today, so i'd have all the help i'd need by the time i'd get home and could try the codes. My apologies if i was sounding ungrateful.

Queue
Posts: 31
Joined: 16 Feb 2013 14:31

Re: Batch scripts queueing loop script

#12 Post by Queue » 27 Feb 2013 14:40

If you have timeout available, and you're only going to use this batch file on computers where you know timeout is available, just use timeout instead of ping. Using ping as a sleep/wait command is a compatibility hack mainly for XP.

Queue

KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Re: Batch scripts queueing loop script

#13 Post by KgOlve » 27 Feb 2013 14:45

I just tried the code, however it just keeps looping again and again even though there are none of the specific process running for any user, nor for the system. The (modified) code i tried was

Code: Select all

##some unrelevant script over here

:queue
for /f %%a in ('tasklist ^|find /i "process.exe" ^|find /c /v "" ') do (
if %%a GTR 2 do (
timeout 60
goto:queue
)
if %%a LSS 2 goto:continue
)

:continue
##rest of the script under here

Queue
Posts: 31
Joined: 16 Feb 2013 14:31

Re: Batch scripts queueing loop script

#14 Post by Queue » 27 Feb 2013 14:53

Are you changing "process.exe" to be the name of the process you're checking for?

Also, GTR 2 should be GEQ 2 (if you want it to match LSS 2, and you can just use else instead, or omit the goto:continue entirely if :continue is immediately after the for loop).

Not that either of those would explain a lack of running child processes.

Since you haven't shown us any of the rest of your batch file, we can only guess as to how you're starting child processes and the control flow of the rest of your batch; we're not psychic.

Queue

KgOlve
Posts: 14
Joined: 30 Oct 2012 06:34

Re: Batch scripts queueing loop script

#15 Post by KgOlve » 27 Feb 2013 15:06

Yes, i change the process.exe to the name of the actual process in question :P

As for the code, this is the start of it

Code: Select all

@echo OFF
cd /D C:\downloads\
timeout 5
set fn=%~n1
echo file: %~nx1
set fne=%~nx1

:queue
for /f %%a in ('tasklist ^|find /i "process.exe" ^|find /c /v "" ') do (
if %%a GTR 2 do (
timeout 60
goto:queue
)
if %%a LSS 2 goto:continue
)

:continue
if exist "%fn%.avs" goto:secondary
##and then it continues on

(I have to use the "cd /d" at the beginning, since for some reason a couple of days ago cmd stopped starting in C:\windows\system32, and instead just starts in D:\)

I also just tried with this code, but with the same result as the one above, it would just loop continuously:

Code: Select all

:queue
for /f %%a in ('tasklist ^|find /i "x264.exe" ^|find /c /v "" ') do (
if %%a GEQ 2 do (
timeout 60
goto:queue
)
)

Last edited by KgOlve on 27 Feb 2013 15:07, edited 1 time in total.

Post Reply