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
  • Can you give more details, code, etc May 30, 2017 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, 2017 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, 2017 at 21:07
  • Its like flock python <script_name>.py cron entry is such that this job runs every minute. @roaima
    – wayfare
    Jun 1, 2017 at 2:39
  • 1
    Next time please put the information in your question. I've done it for you this time.
    – roaima
    Jun 1, 2017 at 7:48

1 Answer 1


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, 2017 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. May 31, 2017 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, 2017 at 20:35
  • 1
    Then how can you safely unlink the flock file if you have many accumulated? Apr 22, 2019 at 20:29
  • 1
    You will have to unlink the files at a moment when you are sure no locking process has a chance of running (e.g. a maintenance phase on the server, with all relevant services shut down, cron disabled, etc). Apr 19, 2020 at 16:45

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