How do I can linux flock command to prevent another root process deleting a file?

Posted by Danmaxis on Stack Overflow See other posts from Stack Overflow or by Danmaxis
Published on 2009-06-24T20:35:24Z Indexed on 2010/05/24 11:41 UTC
Read the original article Hit count: 216

Filed under:
|
|
|

Hello there, I would like to prevent one of my root process from deleting a certaing file. So I came across the flock command, it seems to fit my need, but I didnt get its sintax.

If I only indicate a shared lock, it doesnt work:
flock -s "./file.xml"

If I add a timeout parameter, it still doesnt work
flock -s -w5 "./file.xml"

It seems that way, it fits in flock [-sxun][-w #] fd# way.
(What is this fd# parameter?)

So, I tried the flock [-sxon][-w #] file [-c] command
Using flock -s -w5 "./file.xml" -c "tail -3 ./file.xml" and it worked, tail command at ./file.xml was executed.
But I would like to know, does the lock end after the command or does it last 5 seconds after the end of the command execution? My main question is, how can I prevent another root process deleting a file in linux?

© Stack Overflow or respective owner

Related posts about linux

Related posts about bash