Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is flock automatically released on process exit?

In a bash script in Linux, I am using flock [the command flock, not the system call flock()] to implement file locking thereby guarding concurrent access against a shared resource [which is a file in tmpfs].

I have trap handlers to handle abnormal termination of my script: trap "{ rm -rf $LOCK ; rm -rf $TMPFS_FILE; exit 255; }" SIGINT SIGTERM

where $LOCK is my lock file and $TMPFS_FILE is my shared resource.

My question is do I need to explicitly do a file unlock as well ? Or does Linux do it for me upon all program termination [both voluntary termination as well as forced] scenarios ?

like image 239
Bandicoot Avatar asked Jun 11 '12 21:06

Bandicoot


1 Answers

From man 1 flock:

-u, --unlock

          Drop  a  lock.   This  is  usually not required, since a lock is
          automatically dropped when the file is closed.  However, it  may
          be  required  in special cases, for example if the enclosed com‐
          mand group may have forked a background process which should not
          be holding the lock.
like image 180
Dennis Williamson Avatar answered Sep 22 '22 23:09

Dennis Williamson