user2154424
user2154424

Reputation: 31

Running multiple instances of a Batch file in windows simultaneously?

I have a windows batch file that is invoked by windows scheduler. When I try to have multiple windows scheduler tasks trying to run the batch file simultaneously, the batch file is locked by the first process and the all the other instances fail.

Is there is way in Windows to run multiple instances of batch file simultaneously?

My script is a simple one all it does is:

set java_classpath
java javaClass

Upvotes: 0

Views: 4965

Answers (4)

Matthias
Matthias

Reputation: 3920

to people coming here from google simply looking for a way to run multiple instances of a .bat file simultaneously, a simple way would be this script:

set N=3
for /L %%i in (1,1,%N%) do (
  start yourscript.bat
)

Upvotes: 0

peet
peet

Reputation: 274

Did you try calling your batchfile by using %systemroot%\cmd.exe /K C:\path\batchfile.bat? With /K each time a new instance of cmd is opened, guess it is the shell not the file making you weird.

Upvotes: 0

MKaama
MKaama

Reputation: 1939

Windows 8 task scheduler has the following option (on the last, "Settings" tab): If the task is already running, then the following rule applies:

  • Do not start a new instance (default)
  • Run a new instance in parallel
  • ...

Probably you should change this setting. And also, I would suggest you look into http://serverfault.com and post there.

Upvotes: 0

dbenham
dbenham

Reputation: 130869

There is nothing inherent to batch file mechanics that limits the number of processes that can simultaneously run the same script. The actual batch script is not locked when it is run. In fact, it is possible to modify a batch script while it is running, though that is usually a very bad idea.

But a batch script could take any number of actions that would prevent simultaneous runs. The most obvious is if the script attempts to redirect output to a specific file (constant path and name). The output redirection establishes an exclusive lock that will prevent any other process from obtaining the same lock.

Another possibility is your script could be calling an external command or program that establishes an exclusive lock in some way.

Either way, there should be nothing to prevent multiple processes from launching the same script simultaneously. But if the script establishes an exclusive lock, then one (or more) of the instances may either crash or exit prematurely, or seem to hang, depending on how the failed lock aquisition is handled.

There really isn't any way to be more specific unless you post your actual script. But if it is a long script, then you should attempt to isolate where the problem is occurring before posting.

Upvotes: 1

Related Questions