5

After process is completed, I see that the lock file isn't deleted? Is there any reason that why flock keeps the file ? Also how does flock knows if there is a lock acquired ?

Here is the example from a crontab file

* * * * * flock python <script_name>.py

closed as too broad by user34720, Kusalananda, Jeff Schaller, roaima, phk May 31 '17 at 7:59

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can you give more details, code, etc – Romeo Ninov May 30 '17 at 18:09
  • I saw flock being used in a shell script. The task is executed by cron and if it is already running then it should not run it. – wayfare May 30 '17 at 20:18
  • There are different methods for shell-script based locking. Can you extract the relevant part of the code and include it in your question, please. (Ideally just a few lines, with something like ... thrown in where appropriate.) – roaima May 30 '17 at 21:07
  • Its like flock python <script_name>.py cron entry is such that this job runs every minute. @roaima – wayfare Jun 1 '17 at 2:39
  • 1
    Next time please put the information in your question. I've done it for you this time. – roaima Jun 1 '17 at 7:48
9

For most use cases of flock, it's very important that the lock file not be "cleaned up". Otherwise, imagine this scenario:

  • process A opens the lock file, finds it does not exists, so it creates it.
  • process A acquires the lock
  • process B opens the lock (finds it already exists)
  • process B tries to acquire the lock but has to wait
  • process A releases the lock
  • process B acquires the lock instantly
  • process A deletes the lock file
  • process C opens the lock file, finds it does not exists, so it creates a new one. Note that it is now holding open a different lock file that the one that process B has locked.
  • process C tries to acquire the lock and succeeds... but it should have had to wait, because process B still has [a prior incarnation of] the lock file open and locked.
  • Thanks. I was not aware that flock waits to get a lock. Do you know if its like a queue ? What happens when I have 10 tasks who needs lock to same resource ? – wayfare May 30 '17 at 20:16
  • 3
    @wayfare What happens when you have 10 processes waiting for the same resource is implementation-dependent. The important point is that only one of them can get said resource at any given time. – Satō Katsura May 31 '17 at 7:13
  • @wayfare flock waiting to get the lock is optional. You can also ask it to fail immediately instead of waiting in case the lock is not available. But the same problem remains. – Celada Jun 1 '17 at 20:35
  • Then how can you safely unlink the flock file if you have many accumulated? – user3019105 Apr 22 at 20:29

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