-1

I see in some batch file (let's name it script.bat) a command like

call some_command.bat < .r.lock

As I understand ".r.lock" is a lock file that shouldn't let new processes started using that lock file. However in my experiments I'm able to start two instances of script.bat simultaneously so I'd like to learn more about this pattern of using (< lock_file). Could anyone please confirm that my understanding of usage of "< lock_file" is correct - preventing other instances to run on the same lock file and if anyone knows where I can read about such approach.

Thanks, Valery

ValeryC
  • 477
  • 3
  • 17
  • Can you please provide, in your question, the reference material / links to support your understanding. That way members without your understanding can improve their knowledge and those with the knowledge can verify or contradict yours! – Compo Aug 26 '19 at 13:09

2 Answers2

0

if I recall this correctly, the < in the code is probably used for input redirection. Therefore it is possible to process input from the file instead of the STDIN.

It's not by default a locking mechanism. You could for instance use the data in this file as input for the batch and create a sort of locking algorithm based on this data.

  • Thanks Christiaan, Yes, I also thought before I saw that that "<" is for input redirection only. ".r.lock" contains a single character meaningless for the program. It looks like there is more than just input - it looks like that the program "some_command.bat" starts again after sending Ctrl-Del to the command window. Probably this is also an auto-restart mechanism. Not sure. – ValeryC Aug 26 '19 at 13:27
  • Quick update - it is defenetly an auto-restart mechanism. Tested with JBoss server 1. created an .r.lock file and executed command standalone.bat < .r.lock server started up 2. Sent Ctrl-C in the command window 3. JBoss server shut down and immmediately went starting up. The remaing question for me is where to read about such approach – ValeryC Aug 26 '19 at 13:34
  • Ah... this clarifies a bit :-) It's probably the file that jboss application server uses to lock the execution to a single instance ;-) – Christiaan Nieuwlaat Aug 26 '19 at 13:49
0

You are confused or misremembering code, or else the code was incorrect.

Redirecting stdin to a file does not lock the file. Any number of process can read from the same file. In addition, one other process could write to the file while it is open for reading.

If you want to lock a file in batch then you want to redirect output to the file.

If you don't care about existing content, then you can use >lock.txt. If you want to preserve existing content then >>lock.txt.

The file will be exclusively locked for the duration of the associated command. While locked, no other process can write to the file (redirected output to that file will fail for other processes). But any number of processes can still read from the file while it is locked.

For a simple test, issue pause >lock.txt from one console window. The lock.txt file will remain locked until you press a key in that window. Now from another console window attempt to write to the file with any command that you like - it will fail. For example echo hello >lock.txt will fail.

But you can still read from the file while it is locked. For example, type lock.txt will output Press any key to continue....

It is possible for a locked file to function as a queue or messaging service, in which case it might make sense to also have code that uses <lock.txt.

Here are some links discussing applications of file locking in batch:
How do you have shared log files under Windows?
Parallel execution of shell processes
How to check in command-line if a given file or directory is locked (used by any process)?

dbenham
  • 127,446
  • 28
  • 251
  • 390