del_client.sh --delete client1 &
del_client.sh --delete client2 &
del_client.sh --delete client3 &
del_client.sh --delete client4 &

When I executed the commands above in the background, client1 was not deleted.


code which is called during the operation above:


cat $FILE |grep -v ^"${entry}\." | grep -v ^"# ${entry} VT" > $FILE.del
        cp -p $FILE.del $FILE
  • 1
    I don't know how $FILE is determined inside the script but it sounds like all 4 invocations of the script might be manipulating the same file at the same time. Since you do not serialize access to the file with a lock, that would explain your result since you would then be facing a race condition. – Celada Mar 28 '16 at 10:42
  • I agree. That's why i am trying to implement locking mechanism. – Gaurav KS Mar 28 '16 at 10:54
  • 1
    You don't show any code that would delete a file, why do you expect one to be deleted? How is $FILE set? How is $entry set? Ho can we know where the error is if you don't show us your script? Please edit your question and give us more information. – terdon Mar 28 '16 at 10:55

Your question doesn't say where $FILE gets its value from, but it sounds like every invocation of the script is actually manipulating the same $FILE.

If that's true, then your 4 invocations are racing with each other and the failure you see is expected. Because you carry out a non-atomic read+modify+replace operation on this file and you don't use mutual exclusion to make sure only one process is in its critical section at a time, one of the invocations of the file can easily clobber another's modifications.

You should run del_client_entry under a lock. For example:

    flock 9
    del_client_entry <parameters>
) 9>/my/lock/file
  • Initially i was giving same file name, in which i was making changes in /my/lock/file ,so it was not working. Later i gave 9>/tmp/lck, then it worked. Thanks a lot Celada :) – Gaurav KS Mar 29 '16 at 5:13

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.