6

I have a shell script which will be executed by multiple instances and if an instance accessing a file and doing some operation how can I make sure other instances are not accessing the same file and corrupting the data ?

My question is not about controlling the parallel execution but dealing with file lock or flagging mechanism.

Request some suggestion to proceed.

8

Linux normally doesn't do any locking (contrary to windows). This has many advantages, but if you must lock a file, you have several options. I suggest

flock: apply or remove an advisory lock on an open file.

This utility manages flock(2) locks from within shell scripts or from the command line.

For a single command (or entire script), you can use

flock --exclusive /var/lock/mylockfile -c command

If you want to execute more commands in your script under the lock, use

#!/bin/bash
 .... 

(
flock --nonblock 200 || exit 1
# ... commands executed under lock ...
) 200>/var/lock/mylockfile 

All operations following the flock call inside the sub-shell (...) are executed only if the no other process currently holds a flock on /var/lock/mylockfile. The lock is automatically dropped after the sub-shell exited.

flock can also wait until the file lock has been dropped (that's the default). In this case do not use the --nonblock option, which makes flock fail if no successful lock can be obtained.

| improve this answer | |
  • A little note: advisory locks are, as the name suggest, advisory. Your script instances must cooperate, that is, always check for the lock's status before manipulating the file. Nothing actually stops them from reading whenever they want, the lock is just an information. – John WH Smith Oct 28 '14 at 11:22
  • @JohnWHSmith thanks for this addition, I forgot to mention it. – Sebastian Oct 28 '14 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.