0

Is it possible for a batch program and shell script to know if a previous instance of itself is still running and wait until that program ends before triggering the next step in the command line?

We have a bat file and a corresponding shell that is triggered by an application which writes logs into a file, what happens when multiple users use the same application and triggers the program at the same time is that the logs get jumbled and is not readable by the program that turns the log into a PDF.

Thanks.

Simon T
  • 53
  • 1
  • 10
  • There are several possible solutions here. What have you tried? – Eric Nov 02 '16 at 02:05
  • @Eric, I have tried putting a wait clause but it does not have the option to wait over and over. I do not know how to only wait if the same bat or sh file is still working in a different instance. – Simon T Nov 02 '16 at 02:57
  • I've also tried the suggestion here: [link](http://stackoverflow.com/questions/8177695/how-to-wait-for-a-process-to-terminate-to-execute-another-process-in-batch-file ) but this allows multiple instance to run at the same time which means it defeats the purpose of having it wait for itself to finish. – Simon T Nov 02 '16 at 05:46
  • One other thing you can try is to use a file lock. See: [How to lock a file in Windows without changing it or using third party tools?](http://superuser.com/questions/857725/how-to-lock-a-file-in-windows-without-changing-it-or-using-third-party-tools/857735#857735). You check for the lock at the start of the script, wait until it is cleared, then grab the lock and run. There are two main hazards here: A) you have to be sure to release the lock, even if your script fails, B) multiple instances of the script might think they've got the lock if started simultaneously -- it's a race condition. – Eric Nov 02 '16 at 11:04
  • @Eric I was fiddling around with lockfiles actually. Since a file can technically lock itself until it is finished. The problem I'm getting is that I can't loop the process from the outside so the other requests wait rather than just ending. – Simon T Nov 03 '16 at 00:17

0 Answers0