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 ...
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 ...
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 ...
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 ...
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 ...
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 $$"
...
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
...
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 ...
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 ...
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.
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 ...
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 ...
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 ...
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 ...
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 ...
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
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 ...
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 (-...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
flock × 31shell-script × 9
bash × 8
lock × 8
shell × 3
linux × 2
ubuntu × 2
cron × 2
rsync × 2
c × 2
file-descriptors × 2
subshell × 2
concurrency × 2
centos × 1
ssh × 1
command-line × 1
permissions × 1
zsh × 1
btrfs × 1
debugging × 1
synchronization × 1
exec × 1
xubuntu × 1
syntax × 1
fork × 1