Questions tagged [flock]
The flock tag has no usage guidance.
26
questions
0
votes
0
answers
153
views
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?
1
vote
0
answers
161
views
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 ...
0
votes
2
answers
62
views
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 ...
0
votes
1
answer
549
views
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
...
-2
votes
1
answer
162
views
Are there any plans for Linux to add higher-level things like Windows' WaitForMultipleObjects?
WaitForMultipleObjects is one of several Windows kernel functions that can suspend and synchronize a calling thread with other threads until resources or etc are available, similar to flock in Linux, ...
0
votes
1
answer
159
views
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 ...
0
votes
2
answers
84
views
How to run eval with lockf command?
I have a command which I run via eval as shown below.
#! /bin/sh
readonly scr="MYENV=1 sh /tmp/scr.sh"
eval ${scr} -a 1 -b 2
Now I want to run the scr script with lockf utility, so I made ...
1
vote
0
answers
289
views
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 ...
-3
votes
2
answers
968
views
is there a really simple and reliable way to create a unique lock (file) on linux? without using `flock` [closed]
EDIT: I learned how to use flock for exclusive lock and how to not mess with it: https://superuser.com/questions/1619940/flock-is-randomly-failing-on-desktop-pc-but-not-on-notebook-could-be-defectiv/. ...
9
votes
3
answers
6k
views
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 ...
0
votes
1
answer
180
views
rsync script work on CentOS 7, same script doesn't work on RHEL 7
I have a VM cluster with 3 nodes on CentOS7 and one node on RHEL7. There is a directory where rsync is enabled /mnt/<server_IP>/portal/wso2telcohub-3.0.2/repository/deployment/server/synapse-...
0
votes
2
answers
433
views
Can we tell if a command is being run by a process or not, by looking at the flock lock file alone?
Is util-linux's flock implemented based on flock() in Linux C API?
Can we tell if a command is being run by a process or not, by looking at the lock file alone?
I found that when a command guarded by ...
0
votes
1
answer
715
views
bash - use exec for file descriptors using environment variables
LOCK_FD=200
LOCK_FILE=/tmp/lock-file
exec ${LOCK_FD}>${LOCK_FILE}
I get an error
exec: 200 not found
This however works
LOCK_FD=200
LOCK_FILE=/tmp/lock-file
eval "exec ${LOCK_FD}>${LOCK_FILE}...
1
vote
2
answers
694
views
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 ...
0
votes
1
answer
1k
views
Why can flock(1) do inter-process lock?
flock is associated with open-file-description, and open(2) creates new open-file-description. So invoking flock(1) in two separate shells will open the lock file separately and result in two open-...
2
votes
1
answer
3k
views
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 ...
8
votes
1
answer
13k
views
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 ...
19
votes
1
answer
13k
views
Why flock doesn't clean the lock file? [closed]
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 ...
0
votes
3
answers
1k
views
crontab not executing script
crontab:
*/5 * * * * /home/owner/scripts/upload_main.sh
upload_main.sh:
#!/bin/bash
flock -xn /home/owner/locks/upload.lck -c /home/owner/scripts/upload_silent.sh
upload_silent.sh:
#!/bin/bash
...
3
votes
1
answer
2k
views
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 ...
0
votes
1
answer
259
views
flock command script fail on xunbuntu 16.04 - cant understad why
#!/bin/bash
(
flock -n 200 || exit 1
# commands executed under lock
sleep 3
echo "TEST"
) 200 > /home/nis/Scripts/lock.txt
Running this scrip gets me this error:
lock.sh: 7: lock.sh: ...
0
votes
1
answer
3k
views
lock bash script to prevent parallel running from within the script itself?
I am aware of using flock and solo (not sure the difference though) when running from a cron job. However, is it possible to prevent parallel running from within the script itself?
0
votes
1
answer
463
views
Duplication of a file descriptor associated with an fcntl lock?
According to the man page of dup2, this syscall make a new copy of the old file descriptor. The two descriptors do not share (the close-on-exec flag).
However in case the file descriptor we are ...
6
votes
1
answer
20k
views
How to do locking a file from a shell script? [duplicate]
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?
5
votes
3
answers
4k
views
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;
...
14
votes
4
answers
11k
views
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, ...