0

I have a Windows batch script (my.bat) which has the following line:

DTBookMonitor.exe 2>&1 > log\cmdProcessLog.txt

So, from my understanding, this runs DTBookMonitor, redirects STDERR to STDOUT and then redirects STDOUT to the file log\cmdProcessLog.txt.

I then run my.bat. DTBookMonitor runs for a significant amount of time, and when I run my.bat a second time (while it is already running), it immediately exits from the second instance of my.bat.

Is this purely because of the redirection to cmdProcessLog?

David Weiser
  • 5,190
  • 4
  • 28
  • 35

2 Answers2

2

Better late then never :)

Windows redirection locks the output file so that no other process can open the file for writing at the same time. That is why the second instance fails when it tries to redirect output to the same file.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Do you have a reference? I'd like to look that up. – David Weiser May 25 '12 at 16:46
  • @DavidAnn: There must be docs somewhere, but I've never seen them. It's just something I've always been aware of going all the way back to the days of DOS. I have sample code showing the locks in action: http://stackoverflow.com/a/10520609/1012053. You can serialize concurrent batch processes using the locks: http://stackoverflow.com/a/9344547/1012053, http://stackoverflow.com/a/9048097/1012053, http://www.dostips.com/forum/viewtopic.php?p=12454 – dbenham May 26 '12 at 01:31
1

I'd guess it's either due to that, or because DTBookMonitor only allows one instance of it to run at a time. The following test should shed some light on the situation:

  • Run the first (long) instance of DTBookMonitor
  • Run a second instance without redirecting any of its output
  • Alternatively, run a second instance, but redirect the output to a file other than log\cmdProcessLog.txt

Do you get similar results? Different results?

Philip Kelley
  • 39,426
  • 11
  • 57
  • 92