Questions tagged [flock]
26 questions
19
votes
1 answer
Why flock doesn't clean the lock file?
After process is completed, I see that the lock file isn't deleted? Is there any reason that why flock keeps the file ? Also how does flock knows if there is a lock acquired ?
Here is the example from a crontab file
* * * * * flock python…
wayfare
- 293
- 1
- 2
- 5
14
votes
4 answers
Pass multiple commands to flock
flock -x -w 5 ~/counter.txt 'COUNTER=$(cat ~/counter.txt); echo $((COUNTER + 1)) > ~/counter.txt'
How would I pass multiple commands to flock as in the example above?
As far as I understand, flock takes different flags (-x for exclusive, -w for…
d-_-b
- 1,167
- 5
- 18
- 27
8
votes
3 answers
Handling of stale file locks in Linux and robust usage of flock
I have a script I execute via cron regularly (every few minutes). However the script should not run multiple times in parallel and it sometimes runs a bit longer, thus I wanted to implement some locking, i.e. making sure the script is terminated…
hanno
- 121
- 1
- 2
8
votes
1 answer
Flock doesn't seem to be working
I've recently been trying to create a shell script for a minor project, and for some reason, the flock command isn't working for me properly. Whenever I invoke it in a subshell the atomic way and put it into the background, other programs seem to…
Mr. Minty Fresh
- 511
- 1
- 4
- 13
6
votes
1 answer
How to do locking a file from a shell script?
I would like to synchronize two Bash scripts via a file lock. How to do this? Zsh has zsystem flock call, but is there a more "shellish" method that's also available to Bash?
Itzie
- 139
- 1
- 1
- 4
5
votes
3 answers
Flocking a filedescriptor in a shell script
I thought this would give me uninterrupted begin-end pairs,
but it doesn't:
#!/bin/bash
fun()(
flock 1 || { echo >&2 "$BASHPID: FAIL: $?"; exit 1; }
echo "$BASHPID begin"
sleep 1;
echo "$BASHPID end"
)
fun &
fun &
fun &
fun &
fun…
Petr Skocik
- 28,176
- 14
- 81
- 141
3
votes
1 answer
Check and Test Lock from other Process
I am trying to create a service wrapper (init.d script) around one of my favorite applications. The application creates both a PID and a lock file, and so I'm trying to use those to ensure that I can report accurate status of the application and…
palswim
- 4,919
- 6
- 37
- 53
2
votes
1 answer
Cannot get bash flock to work
I derived a sample bash script from what I have seen around, regarding bash flock function. I do:
func()
{
42>/home/foo
flock -e 42 || exit 1
echo "hello world"
sleep 5
}
Then I consecutively run func&, each of which prints hello…
corsel
- 403
- 4
- 13
1
vote
0 answers
Ubuntu user not affected by sticky bit on Ubuntu 22.04
I experience a strange behaviour in stick bit on /tmp directory and flock command. Tried with two cases:
Case 1: create file with Ubuntu user, root have no access to the created file.
ubuntu@:~$ touch -a /tmp/ubuntu_user_created.lck
ubuntu@:~$ flock…
Tien Dung Tran
- 131
- 3
1
vote
0 answers
Is this flock usage with if-else safe?
I have two shell scripts that may be run in parallel but may also be run one at a time. Both scripts rely on an initial setup step that takes a while and can't be run by both at the same time.
To allow for parallel execution for the remainder, I've…
Etheryte
- 217
- 1
- 9
1
vote
2 answers
Using flock fails with zsh but works in bash?
Employing the mechanism from the answer at https://unix.stackexchange.com/a/274499/5132 in the Z shell:
(
flock -x 200
echo "test";
) 200>mylockfile2
returns
zsh: parse error near `200'
While in bash it works correctly. What may be the issue…
Chris Stryczynski
- 5,178
- 5
- 40
- 80
0
votes
0 answers
Force running a script using flock?
If given a lock on a script using flock(), is it possible to make a force run/unlock based on some argument passed to the script?
0
votes
2 answers
Shared locking of scripts that may call each other
This is unusual problem and probably it is the consequence of bad design. If somebody can suggest anything better, I'd happy to hear. But right now I want to solve it "as is".
There is a bunch of interacting scripts. It doesn't matter for the sake…
Nikita Kipriyanov
- 1,398
- 8
- 13
0
votes
1 answer
Testing file locking
I have a script which locks a file to avoid concurrent access to it, How can I execute this same script from two different terminals synchronously, to check if it works?
Here is the script
#!/bin/bash
(
flock -xn 200
trap 'rm…
Rob
- 101
0
votes
1 answer
Synchronizing access to shared, remote resource
I have a shared cache on a remote server that multiple clients are reading and writing to, so I need to synchronize access to this cache. I imagine I could:
SSH into the remote and acquire a flock on the server
Push the update to the server…
mola
- 101
- 2