Skip to main content
12 votes

Handling of stale file locks in Linux and robust usage of flock

An flock lock is associated with a file description object; it will go away once all file descriptors referring to the file description have been closed (see the flock.2 manpage). If the file is still ...
thejh's user avatar
  • 373
10 votes
Accepted

Flock doesn't seem to be working

flock does advisory locking, which is a cooperative locking scheme. This means that you will be able to override the lock if you don't cooperate. You cooperate by requesting the lock before doing the ...
Kusalananda's user avatar
  • 356k
7 votes
Accepted

Using flock fails with zsh but works in bash?

Bash is the only shell that allows the user to open a fd higher than 9 directly using the normal redirection syntax. so in other shells the command is equivalent to (...) 200 1>mylockfile2, which ...
llua's user avatar
  • 7,078
4 votes

Handling of stale file locks in Linux and robust usage of flock

I ran into this same problem with flock. thejh's suggestion to use fuser helped me track down the problem. It turned out that the command I ran with flock launched a child process that remained in the ...
Dan Dalton's user avatar
4 votes

Why can flock(1) do inter-process lock?

No, flock, which is a wrapper around the system call flock(), locks on files, not file descriptors. The OpenBSD manual for flock() says (my emphasis): flock() applies or removes an advisory lock on ...
Kusalananda's user avatar
  • 356k
4 votes
Accepted

Cannot get bash flock to work

Consider this example, based on the example from the flock man page: #!/bin/bash func() { echo "$$ trying to acquire lock" ( flock -e 42 echo "lock acquired by $$" ...
Andy Dalton's user avatar
  • 14.7k
3 votes
Accepted

Flock and bash strange chicken and egg problem

Contrast the following: sh -c 'ls -l /proc/$$/fd' 9>/tmp/toto.txt total 0 lrwx------ 1 chris chris 64 Dec 16 11:24 0 -> /dev/pts/0 lrwx------ 1 chris chris 64 Dec 16 11:24 1 -> /dev/pts/0 ...
Chris Davies's user avatar
3 votes
Accepted

What does "trap: SIGINT: bad trap" mean and how do I fix it?

Based on the information you gave in comments, your old server probably used an older release of Ubuntu with bash as the /bin/sh shell. Bash supports signal names like HUP and SIGHUP as arguments to ...
Sotto Voce's user avatar
  • 7,201
3 votes

Using flock fails with zsh but works in bash?

zsh is not bash despite some efforts between the two camps of peeking over the fence and stealing ideas from one another. Also, flock appears to come from util-linux so portability may be limited. A ...
thrig's user avatar
  • 35.8k
3 votes

Can we tell if a command is being run by a process or not, by looking at the flock lock file alone?

Yes. It is explicitly stated in man 1 flock that flock command uses flock() and man 2 flock notes section says that flock() is implemented by system call since Linux kernel 2.0.
sebasth's user avatar
  • 15.8k
2 votes
Accepted

bash - use exec for file descriptors using environment variables

Methinks that's because redirection is performed before variable expansion. man bash is not quite clear which is done first: REDIRECTION Before a command is executed, its input and output ...
RudiC's user avatar
  • 9,049
2 votes
Accepted

Shared locking of scripts that may call each other

If none of your scripts requires or expects command-line options, you could use a command-line option to indicate whether a "subordinate" script is to use the flock mechanism or not. So, in ...
AdminBee's user avatar
  • 23.6k
1 vote

When I use flock it exits immediately instead of waiting

It took me a long time to figure out that I misunderstood and made a dangerous assumption. I assumed from seeing it called with -n 100 to specify fd 100, that -n was the flag to set the file ...
Steven Lu's user avatar
  • 2,432
1 vote

Shared locking of scripts that may call each other

Each script should check the existence (in practice: emptiness vs. non-emptiness) of a dedicated environment variable. Pick an unused name for the variable. If the variable exists then the script ...
Kamil Maciorowski's user avatar
1 vote

How to run eval with lockf command?

readonly scr="MYENV=1 sh /tmp/scr.sh" eval ${scr} -a 1 -b 2 Here, you'd probably be better off with a function: scr() { MYENV=1 sh /tmp/scr.sh "$@" } scr -a 1 -b 2 That still ...
ilkkachu's user avatar
  • 148k
1 vote

How to run eval with lockf command?

eval is a shell builtin. In general using it is discouraged as it is easy to get things wrong. You probably just need #!/bin/sh MYENV=1 lockf -k /tmp/f.lock sh /tmp/scr.sh -a 1 -b 2
icarus's user avatar
  • 19.1k
1 vote
Accepted

rsync script work on CentOS 7, same script doesn't work on RHEL 7

I advised you to enable error reporting to a log file /tmp/errors by adding a new command at line two. Extending this a little, we get this modification that captures expected output and unexpected ...
Chris Davies's user avatar
1 vote

Can we tell if a command is being run by a process or not, by looking at the flock lock file alone?

$ flock -xn /home/vagrant/forever.lck -c /var/tmp/forever.sh The above flock command will open the /home/vagrant/forever.lck file with an exclusive (-x) file lock and execute the specified command (-...
Tim K's user avatar
  • 11
1 vote

Flocking a filedescriptor in a shell script

I think the point is: file descriptor 1 is reserved for stdout and already used. we should use other than stdin/out/err like 9 as man 1 flock example shows. flock with file descriptor needs to open ...
Fumisky Wells's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible