Comment on Show HN: Linux CLI tool to provide mutex locks for long running bash opsparentComments−Zacru1yBecause that's a syscall ;) https://man7.org/linux/man-pages/man1/flock.1.html is the command line manual.I would say one good reason is that waitlock myapp & JOB_PID=$! # ... do exclusive work ... kill $JOB_PID is a lot easier to use and remember than (; flock -n 9 || exit 1; # ... commands executed under lock ...; ) 9>/var/lock/mylockfile−yjftsjthsd-h1yWhy (; flock -n 9 and not ( flock -n 9 ?−apopapo1yIt's a "for" loop.−marklgr1yCould you elaborate?−apopapo1yA for loop in a shell script may sometimes look like this:`for ((i = 0 ; i < max ; i++ )); do echo "$i"; done`Here this is essentially a "while" loop, meaning it will keep executing the commands as long as we don't reach `exit 1`.(; flock -n 9 || exit 1; # ... commands executed under lock ...; )−yjftsjthsd-h1yIt doesn't seem to work? [~] 0 $ ( flock -n 9 || exit 1; echo in loop ; sleep 3 ; echo done working ; ) 9>~/tmp/mylock in loop done working [~] 0 $ (; flock -n 9 || exit 1; echo in loop ; sleep 3 ; echo done working ; ) 9>~/tmp/mylock -bash: syntax error near unexpected token `;' [~] 2 $ (This is bash)−permalac1yFlock can be used in a single line for example for cronjobs.Flock -s file && script.Pretty simple. (I forgot the argument, I think is -s..−bigattichouseOP1yjust pushed a change so now it's:waitlock myapp & #... do stuff waitlock --done myapp
Comments
Because that's a syscall ;) https://man7.org/linux/man-pages/man1/flock.1.html is the command line manual.
I would say one good reason is that
is a lot easier to use and remember thanWhy
and notIt's a "for" loop.
Could you elaborate?
A for loop in a shell script may sometimes look like this:
`for ((i = 0 ; i < max ; i++ )); do echo "$i"; done`
Here this is essentially a "while" loop, meaning it will keep executing the commands as long as we don't reach `exit 1`.
(; flock -n 9 || exit 1; # ... commands executed under lock ...; )
It doesn't seem to work?
(This is bash)Flock can be used in a single line for example for cronjobs.
Flock -s file && script.
Pretty simple. (I forgot the argument, I think is -s..
just pushed a change so now it's:
waitlock myapp & #... do stuff waitlock --done myapp