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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With