Usecase: I have a shell command I'd like to execute once on a bunch of Consul agent hosts. I only want a concurrency level of X to not choke the system. This sounds like a perfect usecase for a semaphore. However, implementing semaphore for a shell script is cumbersome. consul lock supports semaphore, but it will restart my shell command over and over again.
Proposal: Add a consul lock -oneoff parameter that acquires a semaphore, executes the command and then exits. What do you think?
Workaround: I could use the restarting consul lock my-script.sh and have my script do something like:
#!/bin/sh
# Check if file XXX exists. If so, sleep 5 seconds (to not spin CPU) and then quit.
# Create the file XXX
# Run my command
A different workaround is to have the script kill the consul lock command (using something like $PPID) when it's done.
name wise -once would be more consistent - i like the idea though :)
;) I'm totally fine with -once.
I got started implementing a shell semaphore (with jq dependency) https://gist.github.com/JensRantil/4173e4f1ecf031f3f263e5a1c2f5f861 Gave up after a while.
Hi @JensRantil have you looked at the consul lock command? If I understand your use case correctly I think it does exactly what you are looking for :-)
consul lock -n=<concurrency> /some/lock/prefix yourscript.sh
This will run up to n at any given time and will simply execute the command and then exit.
@slackpad Slackpad. Rereading the documentation I now see that the consul lock command doesn't restart a command! 馃憤 Closing this!
Most helpful comment
@slackpad Slackpad. Rereading the documentation I now see that the
consul lockcommand doesn't restart a command! 馃憤 Closing this!