wait in batch file

J

JIM.H.

Hello,
I have a batch file and many users should be able to run
it simultaneously on a mapped folder. My problem is when
the users run this file at the same time, one should be
going through next steps and the rest should be waiting
the first user to end. Once that user ends next user
should be executing the steps and the other should be
still waiting again (there might be more than 3 users who
run at the same time, the batch file takes time to run).
How can I do that?
Thanks,
Jim.
 
A

Al Dunbar [MS-MVP]

JIM.H. said:
Hello,
I have a batch file and many users should be able to run
it simultaneously on a mapped folder. My problem is when
the users run this file at the same time, one should be
going through next steps and the rest should be waiting
the first user to end. Once that user ends next user
should be executing the steps and the other should be
still waiting again (there might be more than 3 users who
run at the same time, the batch file takes time to run).
How can I do that?

First, what is the problem with the batch file being run concurrently by
more than one user? If it tromps over temp files created by its other
instances, then you should take steps to ensure that each instance uses its
own unique set of temp files. If the batch modifies a common datafile of
some sort, and that is basically the reason that people would have to run
it, then you will need to do as you suggest above.

There are a number of techniques that could be used to "semaphore" access to
the resources shared through the batch file - the best one to pick probably
depends to some degree on the nature of the batch itself.

One possibility is to create a flag file to indicate "batchfile in use".
Each instance would check for this file and, if found, do some sort of timed
loop until it no longer exists. At that point it would need to create the
file by writing some unique value to it, and then read back the file to see
if it was the one that it tried to create. This would probably require some
method of locking the file while it is being written.

/Al
 
T

Torgeir Bakken (MVP)

Al Dunbar said:
There are a number of techniques that could be used to "semaphore" access to
the resources shared through the batch file - the best one to pick probably
depends to some degree on the nature of the batch itself.

One possibility is to create a flag file to indicate "batchfile in use".
Each instance would check for this file and, if found, do some sort of timed
loop until it no longer exists. At that point it would need to create the
file by writing some unique value to it, and then read back the file to see
if it was the one that it tried to create. This would probably require some
method of locking the file while it is being written.

Hi

One downside: If the script/computer that created the flag file crashes, you
will have a deadlock...
 
T

Torgeir Bakken (MVP)

JIM.H. said:
Hello,
I have a batch file and many users should be able to run
it simultaneously on a mapped folder. My problem is when
the users run this file at the same time, one should be
going through next steps and the rest should be waiting
the first user to end. Once that user ends next user
should be executing the steps and the other should be
still waiting again (there might be more than 3 users who
run at the same time, the batch file takes time to run).
How can I do that?

Hi

I don't know a safe way to do this with a batch file that will handle a
script/computer crash gracefully without creating a deadlock. but I know how to
do it with a VBScript file (using a file lock technique):



Const ForAppending = 8

' file that will be locked
sLckFile = "k:\script.lck"

Set oFSO = CreateObject("Scripting.FileSystemObject")

bUpdFinished = False
iLoops = 0
On Error Resume Next
Do
' open for appending
Set f = oFSO.OpenTextFile(sLckFile, ForAppending, True)
If Err.Number = 70 Then
'Permission denied error
' Waiting 1/2 a second before trying again
WScript.Sleep 500
ElseIf Err.Number <> 0 then
WScript.Echo "Unexpected Error #: " & Err.Number
bUpdFinished = True
Else
On Error GoTo 0
' do the job here, the file will now be locked by this script and
' nobody else will be able to continue until the f.Close instruction
'below is run...

' close the file so others can be able to continue
f.Close
bUpdFinished = True
End If
Err.Clear
Loop Until bUpdFinished
On Error GoTo 0
 
A

Al Dunbar [MS-MVP]

Torgeir Bakken (MVP) said:
Hi

I don't know a safe way to do this with a batch file that will handle a
script/computer crash gracefully without creating a deadlock. but I know how to
do it with a VBScript file (using a file lock technique):

Cool. And by the strangest of coincidences, this might just happen to be the
solution to a problem I am currently working on!

Obviously, when the computer having the lock crashes, the lock will
disappear. What is the latency of this, and where on the server are the
settings located that determine this?

/Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top