add "-c" to flock call so variables get passed to command executed by run-one

Bug #1145461 reported by toobuntu
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
run-one
New
Medium
Dustin Kirkland 
run-one (Ubuntu)
Fix Released
Medium
Dustin Kirkland 

Bug Description

I have a cronjob which, for convenience and safety, requires two variables to be passed to it in production. This is not possible the way run-one calls flock, unless the script is called with sh -c, which presumably forks a new process and does not seem streamlined, or the variables are passed to run-one, too, which does not seem elegant.

The below diff fixes this behavior for me:

$ diff -Nru a/run-one b/run-one
--- a/run-one 2013-03-04 18:02:15.513983439 -0500
+++ b/run-one 2013-03-04 18:24:42.414039276 -0500
@@ -32,6 +32,7 @@
 mkdir -p "$DIR"

 # Calculate the hash of the command and arguments
+CMD="$@"
 CMDHASH=$(echo "$@" | md5sum | awk '{print $1}')
 FLAG="$DIR/$CMDHASH"

@@ -39,7 +40,7 @@
 case "$(basename $0)" in
  run-one)
   # Run the specified commands, assuming we can flock this command string's hash
- flock -xn "$FLAG" "$@"
+ flock -xn "$FLAG" -c "$CMD"
  ;;
  run-this-one)
   ps="$@"
@@ -59,14 +60,14 @@
   [ -z "$pid" ] || kill $pid
   sleep 0.1
   # Run the specified commands, assuming we can flock this command string's hash
- flock -xn "$FLAG" "$@"
+ 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
    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

------------------
This will do it, but I'm not sure if invoking /bin/sh launches a new process to do so, and want to keep system resource usage as minimal as possible:
$ /usr/bin/run-one /bin/sh -c 'DEBUG=n TEST=n /usr/local/bin/rsync-daily.sh'

The following fail because of the way run-one invokes flock and passing the command as "$@", flock tries to create a lock on the first parameter passed to /usr/bin/run-one:
$ /usr/bin/run-one DEBUG=n TEST=n /usr/local/bin/rsync-daily.sh
flock: DEBUG=n: No such file or directory
$ /usr/bin/run-one 'DEBUG=n TEST=n /usr/local/bin/rsync-daily.sh'
flock: DEBUG=n TEST=n /usr/local/bin/rsync-daily.sh: No such file or directory

The following fails because the variables do not get passed to the script:
$ /usr/bin/run-one /usr/local/bin/rsync-daily.sh DEBUG=n TEST=n
status: testing $DEBUG...
+ rsync_opts=-v --progress -ah --stats --delete --numeric-ids
+ printf DEBUG != n, debug mode activated\n
DEBUG != n, debug mode activated
+ printf status: testing $TEST...\n
status: testing $TEST...
+ test y = n
+ rsync_opts=-n -v --progress -ah --stats --delete --numeric-ids
+ printf TEST != n, test mode activated\n
TEST != n, test mode activated

I tried playing around with xargs with no luck. For elegance, I'd rather not pass the variables to run-one itself, though this does work:
$ DEBUG=n TEST=n /usr/bin/run-one /usr/local/bin/rsync-daily.sh
status: testing $DEBUG...
DEBUG = n
status: testing $TEST...
TEST = n
---
ApportVersion: 2.0.1-0ubuntu17.1
Architecture: amd64
DistroRelease: Ubuntu 12.04
InstallationMedia: Ubuntu 12.04 LTS "Precise Pangolin" - Alpha amd64 (20120226)
MarkForUpload: True
Package: run-one 1.7-0ubuntu1 [modified: usr/bin/run-one]
PackageArchitecture: all
ProcVersionSignature: Ubuntu 3.5.0-25.38~precise1-generic 3.5.7.4
Tags: precise
Uname: Linux 3.5.0-25-generic x86_64
UpgradeStatus: No upgrade log present (probably fresh install)
UserGroups: adm cdrom dip lpadmin plugdev sambashare staff sudo

Related branches

Revision history for this message
toobuntu (toobuntu) wrote :
Revision history for this message
toobuntu (toobuntu) wrote : Dependencies.txt

apport information

tags: added: apport-collected precise
description: updated
Revision history for this message
toobuntu (toobuntu) wrote : ProcEnviron.txt

apport information

tags: added: patch wishlist
Revision history for this message
Ubuntu Foundations Team Bug Bot (crichton) wrote :

The attachment "allow passing variables to command to be invoked by run-one" of this bug report has been identified as being a patch. The ubuntu-reviewers team has been subscribed to the bug report so that they can review the patch. In the event that this is in fact not a patch you can resolve this situation by removing the tag 'patch' from the bug report and editing the attachment so that it is not flagged as a patch. Additionally, if you are member of the ubuntu-reviewers team please also unsubscribe the team from this bug report.

[This is an automated message performed by a Launchpad user owned by Brian Murray. Please contact him regarding any issues with the action taken in this bug report.]

Revision history for this message
toobuntu (toobuntu) wrote :

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:

       (
         flock -n 9 || exit 1
         # ... commands executed under lock ...
       ) 9>/var/lock/mylockfile

$ 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
+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

Changed in run-one (Ubuntu):
importance: Undecided → Medium
status: New → In Progress
assignee: nobody → Dustin Kirkland (kirkland)
Changed in run-one:
assignee: nobody → Dustin Kirkland (kirkland)
importance: Undecided → Medium
Changed in run-one (Ubuntu):
status: In Progress → Fix Committed
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package run-one - 1.10-0ubuntu1

---------------
run-one (1.10-0ubuntu1) saucy; urgency=low

  * No changes
  * Re-release, due to minor errors in release script
 -- Dustin Kirkland <email address hidden> Tue, 16 Jul 2013 14:55:49 -0500

Changed in run-one (Ubuntu):
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Related questions

Remote bug watches

Bug watches keep track of this bug in other bug trackers.