Differences between revisions 1 and 2
Revision 1 as of 2011-10-26 15:28:46
Size: 1941
Editor: GreyCat
Comment: separated Duffy example
Revision 2 as of 2012-09-04 14:37:22
Size: 2045
Editor: net66-219-41-161
Comment: Fix some of the most egregious breakage
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
This example was contributed by Charles Duffy. It has been separated from the parent page because the code has several issues that make it dubious. This example was contributed by Charles Duffy (who is now more than a little embarassed by same, and suggests using flock instead). It has been separated from the parent page because the code has several issues that make it dubious.
Line 11: Line 11:
   local PROGRAMNAME="${1:-$DEFAULT_NAME}"    local PROGRAMNAME="${1:-$LOCK_DEFAULT_NAME}"
Line 24: Line 24:
   local PROGRAMNAME="${1:-$DEFAULT_NAME}"    local PROGRAMNAME="${1:-$LOCK_DEFAULT_NAME}"
Line 35: Line 35:
   local PROGRAMNAME="${1:-$DEFAULT_NAME}"    local PROGRAMNAME="${1:-$LOCK_DEFAULT_NAME}"
Line 39: Line 39:
       OTHERPID="$(echo $DIR | egrep -o '[0-9]+$')"
       [ -d /proc/${OTHERPID} ] || rmdir $DIR
       OTHERPID="$(egrep -o '[0-9]+$' <<<"$DIR")"
       [ -d "/proc/${OTHERPID}" ] || rmdir "$DIR"
Line 42: Line 42:
     rmdir /tmp/${PROGRAMNAME}-lock/held && return 0 || return 1      rmdir "/tmp/${PROGRAMNAME}-lock/held" && return 0 || return 1

This example was contributed by Charles Duffy (who is now more than a little embarassed by same, and suggests using flock instead). It has been separated from the parent page because the code has several issues that make it dubious.

  • Are we sure this code's correct? There seems to be a discrepancy between the names LOCK_DEFAULT_NAME and DEFAULT_NAME; and it checks for processes in what looks to be a race condition; and it uses the Linux-specific /proc file system and the GNU-specific egrep -o to do so.... I don't trust it. It looks overly complex and fragile. And quite non-portable. -- GreyCat

     LOCK_DEFAULT_NAME=$0
     LOCK_HOSTNAME="$(hostname -f)"
    
     ## function to take the lock if free; will fail otherwise
     function grab-lock {
       local PROGRAMNAME="${1:-$LOCK_DEFAULT_NAME}"
       local PID=${2:-$$}
       (
         umask 000;
         mkdir -p "/tmp/${PROGRAMNAME}-lock"
         mkdir "/tmp/${PROGRAMNAME}-lock/held" || return 1
         mkdir "/tmp/${PROGRAMNAME}-lock/held/${LOCK_HOSTNAME}--pid-${PID}" && return 0 || return 1
       ) 2>/dev/null
       return $?
     }
    
     ## function to nicely let go of the lock
     function release-lock {
       local PROGRAMNAME="${1:-$LOCK_DEFAULT_NAME}"
       local PID=${2:-$$}
       (
         rmdir "/tmp/${PROGRAMNAME}-lock/held/${LOCK_HOSTNAME}--pid-${PID}" || true
         rmdir "/tmp/${PROGRAMNAME}-lock/held" && return 0 || return 1
       ) 2>/dev/null
       return $?
     }
    
     ## function to force anyone else off of the lock
     function break-lock {
       local PROGRAMNAME="${1:-$LOCK_DEFAULT_NAME}"
       (
         [ -d "/tmp/${PROGRAMNAME}-lock/held" ] || return 0
         for DIR in "/tmp/${PROGRAMNAME}-lock/held/${LOCK_HOSTNAME}--pid-"* ; do
           OTHERPID="$(egrep -o '[0-9]+$' <<<"$DIR")"
           [ -d "/proc/${OTHERPID}" ] || rmdir "$DIR"
         done
         rmdir "/tmp/${PROGRAMNAME}-lock/held" && return 0 || return 1
       ) 2>/dev/null
       return $?
     }
    
     ## function to take the lock nicely, freeing it first if needed
     function get-lock {
       break-lock "$@" && grab-lock "$@"
     }

BashFAQ/045/contrib (last edited 2012-09-04 14:37:22 by net66-219-41-161)