top | item 44590168

(no title)

sluongng | 7 months ago

Why not https://man7.org/linux/man-pages/man2/flock.2.html?

discuss

order

yjftsjthsd-h|7 months ago

Nit: Probably https://man7.org/linux/man-pages/man1/flock.1.html (shell command, not the underlying libc function)

anitil|7 months ago

This was my first thought and I suppose flock(1) could be used to recreate a lot of this. But it does come with some other quality-of-life improvements like being able to list all currently-used locks, having a lock holdable by N processes etc.

Zacru|7 months ago

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

  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-h|7 months ago

Why

  (; flock -n 9
and not

  ( flock -n 9

?

permalac|7 months ago

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..

bigattichouse|7 months ago

just pushed a change so now it's:

waitlock myapp & #... do stuff waitlock --done myapp

ethan_smith|7 months ago

flock is indeed built-in: `flock -xn /tmp/mylock.lock -c "echo running locked command"` does mutex locking in bash. Your tool might offer better ergonomics or features beyond flock's capabilities?