Questions tagged [flock]

Shell: The `flock` utility manages locks in scripts; C programming: flock() applies or removes an advisory lock on an open file.

flock is a command line utility to manage flock advisory locks - i.e., they're a convention instead of a hard restriction.

298 questions
2
votes
1 answer

How to tell which file Apache is waiting for when WCHAN = flock_lock_file_wait

We're having an issue with our webservers in which more and more Apache processes are getting stuck waiting for file lock (caused by PHP flock()) to somehow resolve, but never does. Eventually the site gets slower and slower, and finally stops…
dvanhook
  • 253
  • 1
  • 2
  • 10
2
votes
3 answers

multiple users from editing the same form

I have a php code in which multiple users can edit the form at the same time which is causing a problem to us because the last person who saves the form win the race wiping out other users changes. Currently, I am using JSON to store all…
flash
  • 1,455
  • 11
  • 61
  • 132
2
votes
1 answer

Using flock() on Solaris 5.10

I'm writing a C utility for Solaris 5.10 that uses flock() for file synchronization. My goal is to interoperate with an existing legacy system that also invokes flock() under the hood. According to man pages on my box, it is supported in BSD…
ahawkthomas
  • 656
  • 3
  • 13
  • 26
2
votes
1 answer

Does flock lock the file across processes?

The following code triggers an error, though very rarely, when calling file_get_contents that the file does not exist, even though file_exists was called only a few statements above. I believe that the file got deleted, by a cron job, between the…
Joyce Babu
  • 19,602
  • 13
  • 62
  • 97
2
votes
4 answers

Check if file is locked by concurrent process

I have a process that writes a file using file_put_contents(): file_put_contents ( $file, $data, LOCK_EX ); I have added the LOCK_EX parameter to prevent concurrent processes from writing to the same file, and prevent trying to read it when it's…
Ewout
  • 2,348
  • 1
  • 20
  • 24
2
votes
2 answers

getting scripts to run exclusively

I'm trying to get a bash script to run exclusively -- if another instance of the script is already running, then wait for the other instance to finish before starting the new one. I found some references to flock which sounds like it should do…
HardcoreHenry
  • 5,909
  • 2
  • 19
  • 44
2
votes
2 answers

/bin/sh: 1: Syntax error: word unexpected - Using os.system() from Python

I'm trying to execute a shell command from python. Here's the command : (flock -n 200 || (echo no; exit 1) && (echo yes; cat /home/user/Desktop/instructions.json; >/home/user/Desktop/instructions.json)) 200>>/home/user/Desktop/instructions.json I…
MelKoutch
  • 180
  • 1
  • 12
2
votes
3 answers

PHP flock() for read-modify-write does not work

I have a log file maintained by a PHP script. The PHP script is subject to parallel processing. I cannot get the flock() mechanism to work on the log file: in my case, flock() does not prevent the log file shared by PHP scripts running in parallel…
PaulH
  • 2,918
  • 2
  • 15
  • 31
2
votes
2 answers

Flock command in AIX

when I was trying to use flock in AIX box I was getting flock not found. System admin has installed util-linux package but he said flock executable is not available in AIX. Please let me know how to get flock command in AIX??
2
votes
2 answers

Why does flock removes existing text from a file?

So, I am trying to get an exclusive lock on a text file using Flock and a line of text to that file however while doing so it removes whatever text was there earlier. ( flock -n 200 || exit 1 echo "In critical section" echo text >> file.txt echo…
Vijay Shekhawat
  • 149
  • 2
  • 13
2
votes
0 answers

Strange behavior of flock()

Studying the principles of blocking files, I could not understand why this code works in this way: "); if ($res) { flock($fh, LOCK_UN); …
2
votes
2 answers

Perl Open and Flock timeout

Does the "open" function in Perl automatically checks whether a file is locked and wait for it to be unlocked? if so, how long does it wait or how can I control that period of time? I have 5 forked processes appending data to the same file. Each…
JohnnyLoo
  • 877
  • 1
  • 11
  • 22
2
votes
1 answer

flock fd across child processes : if I use same fd do I risk that it gets assigned to other process lock?

I'm writing a multiprocess bash script that runs several processes (continuously), there are some functions I need to launch, every function has a similar form described in man for flock : function1 () { #.... preamble, variable initialization …
magowiz
  • 59
  • 5
2
votes
1 answer

Does python's fcntl.flock function provide thread level locking of file access?

Python's fcnt module provides a method called [flock][1] to proved file locking. It's description reads: Perform the lock operation op on file descriptor fd (file objects providing a fileno() method are accepted as well). See the Unix manual…
Boaz
  • 25,331
  • 21
  • 69
  • 77
2
votes
0 answers

Bash file locking including flock for subprocesses

I am trying to work through securing my scripts from parallel execution by incorporating flock. I have read a number of threads here and came across a reference to this: http://www.kfirlavi.com/blog/2012/11/06/elegant-locking-of-bash-program/ which…
FocusedEnergy
  • 129
  • 1
  • 12