Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [flock]

The tag has no usage guidance.

0
votes
1answer
29 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 ...
-1
votes
1answer
142 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}...
0
votes
2answers
97 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
1answer
212 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-...
1
vote
1answer
314 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 ...
6
votes
1answer
2k 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 ...
5
votes
1answer
4k 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
3answers
618 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
1answer
564 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
1answer
89 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
1answer
1k 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
1answer
146 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 ...
2
votes
1answer
6k 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
2answers
2k 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; ...
10
votes
3answers
5k 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, ...