Skip to main content

Questions tagged [flock]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-1 votes
1 answer
94 views

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

I moved code to a new virtual server running Ubuntu 22. I have the following (edited to make names shorter) in my crontab so that it runs myCommand only if it is not already running: */1 * * * * ...
ferzle's user avatar
  • 3
2 votes
1 answer
34 views

When I use flock it exits immediately instead of waiting

I was confused for a very long time with the meaning of the -n flag for flock(1). Basically there are many guides for this tool, and often what we see is some command like flock -n 100. Here, fd ...
Steven Lu's user avatar
  • 2,312
2 votes
1 answer
69 views

Flock and bash strange chicken and egg problem

In which order are the distinct steps of this bash command done: (flock -n 9) 9> toto.txt If I do only the subshell part: (flock -n 9) I get this result: flock: 9: Mauvais descripteur de fichier (...
Laurent Lyaudet's user avatar
0 votes
0 answers
189 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?
derwian36's user avatar
1 vote
0 answers
180 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 ...
Tien Dung Tran's user avatar
0 votes
2 answers
78 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 ...
Nikita Kipriyanov's user avatar
0 votes
1 answer
740 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 ...
Rob's user avatar
  • 101
-2 votes
1 answer
215 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, ...
Codemeister's user avatar
0 votes
1 answer
201 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 ...
mola's user avatar
  • 101
0 votes
2 answers
124 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 ...
Rahul Bharadwaj's user avatar
1 vote
0 answers
330 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 ...
Etheryte's user avatar
  • 217
-2 votes
2 answers
1k 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/. ...
Aquarius Power's user avatar
11 votes
3 answers
8k 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 ...
hanno's user avatar
  • 151
0 votes
1 answer
188 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-...
Pradeep Sanjeewa's user avatar
0 votes
2 answers
534 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 ...
Tim's user avatar
  • 103k
0 votes
1 answer
765 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}...
Hari Sundararajan's user avatar
2 votes
2 answers
779 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 ...
Chris Stryczynski's user avatar
0 votes
1 answer
2k 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-...
Timothy Kwok's user avatar
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 ...
corsel's user avatar
  • 463
8 votes
1 answer
14k 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 ...
Mr. Minty Fresh's user avatar
19 votes
1 answer
14k 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 ...
wayfare's user avatar
  • 293
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 ...
omanko's user avatar
  • 1
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 ...
palswim's user avatar
  • 5,277
0 votes
1 answer
272 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: ...
Nis's user avatar
  • 1
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?
eekfonky's user avatar
  • 665
0 votes
1 answer
488 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 ...
Bionix1441's user avatar
6 votes
1 answer
21k 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?
Itzie's user avatar
  • 139
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; ...
Petr Skocik's user avatar
  • 29.1k
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, ...
d-_-b's user avatar
  • 1,187