Here's a slightly better patch, only because man flock(1) clarifies that one can pass only a single command with direct invocation of flock. Below the synopsis are three other quotes from the man page. Only the third form, which is not used by run-one, allows multiple commands to be run.
Pass a single command to the shell with -c.
The first and second forms wraps the lock around the executing a command, in a manner similar to su(1) or newgrp(1).
The third form is convenient inside shell scripts, and is usually used
the following manner:
# Calculate the hash of the command and arguments
+CMD="$@"
CMDHASH=$(echo "$@" | md5sum | awk '{print $1}')
FLAG="$DIR/$CMDHASH"
# Handle run-this-one invocation, by killing matching process first
case "$(basename $0)" in
run-one)
- # Run the specified commands, assuming we can flock this command string's hash
- flock -xn "$FLAG" "$@"
+ # Run the specified command, assuming we can flock this command string's hash
+ flock -xn "$FLAG" -c "$CMD"
;;
run-this-one)
ps="$@"
@@ -58,15 +59,15 @@
pid=$(lsof "$FLAG" | awk '{print $2}' | grep "^[0-9]") || true
[ -z "$pid" ] || kill $pid
sleep 0.1
- # Run the specified commands, assuming we can flock this command string's hash
- flock -xn "$FLAG" "$@"
+ # Run the specified command, assuming we can flock this command string's hash
+ flock -xn "$FLAG" -c "$CMD"
;;
keep-one-running)
backoff=1
while true; do
- # Run the specified commands, assuming we can flock this command string's hash
+ # Run the specified command, assuming we can flock this command string's hash
set +e
- flock -xn "$FLAG" "$@"
+ flock -xn "$FLAG" -c "$CMD"
if [ "$?" = 0 ]; then
# Last run finished successfully, reset to minimum back-off of 1 second
backoff=1
Nitpicking grammar in my own patch...
Here's a slightly better patch, only because man flock(1) clarifies that one can pass only a single command with direct invocation of flock. Below the synopsis are three other quotes from the man page. Only the third form, which is not used by run-one, allows multiple commands to be run.
SYNOPSIS
flock [-sxon] [-w timeout] lockfile [-c] command...
flock [-sxon] [-w timeout] lockdir [-c] command...
flock [-sxun] [-w timeout] fd
Pass a single command to the shell with -c.
The first and second forms wraps the lock around the executing a command, in a manner similar to su(1) or newgrp(1).
The third form is convenient inside shell scripts, and is usually used
the following manner:
( lock/mylockfile
flock -n 9 || exit 1
# ... commands executed under lock ...
) 9>/var/
$ diff -Nru a/run-one b/run-one
--- a/run-one 2013-03-04 18:02:15.513983439 -0500
+++ b/run-one 2013-03-05 15:27:46.635948096 -0500
@@ -32,14 +32,15 @@
mkdir -p "$DIR"
# Calculate the hash of the command and arguments $DIR/$CMDHASH"
+CMD="$@"
CMDHASH=$(echo "$@" | md5sum | awk '{print $1}')
FLAG="
# Handle run-this-one invocation, by killing matching process first
case "$(basename $0)" in
run-one)
- # Run the specified commands, assuming we can flock this command string's hash
- flock -xn "$FLAG" "$@"
+ # Run the specified command, assuming we can flock this command string's hash
+ flock -xn "$FLAG" -c "$CMD"
;;
run-this-one)
ps="$@"
@@ -58,15 +59,15 @@
pid=$(lsof "$FLAG" | awk '{print $2}' | grep "^[0-9]") || true
[ -z "$pid" ] || kill $pid
sleep 0.1
- # Run the specified commands, assuming we can flock this command string's hash
- flock -xn "$FLAG" "$@"
+ # Run the specified command, assuming we can flock this command string's hash
+ flock -xn "$FLAG" -c "$CMD"
;;
keep-one-running)
backoff=1
while true; do
- # Run the specified commands, assuming we can flock this command string's hash
+ # Run the specified command, assuming we can flock this command string's hash
set +e
- flock -xn "$FLAG" "$@"
+ flock -xn "$FLAG" -c "$CMD"
if [ "$?" = 0 ]; then
# Last run finished successfully, reset to minimum back-off of 1 second
backoff=1