Locking mechanism in shell scripts

Put the following script int /usr/local/include and call it lock_process.sh

#!/bin/bash

function lock() {
  if [ $# -ne 1 ];then
    "Process Name missing!"
    exit -1
  fi
  NAME=$1
  LOCKFILE=/tmp/$NAME.lock.$BASHPID
  if [ -f $LOCKFILE ];then
    echo "Lockfile $LOCKFILE exists!"
    exit -1  
  else
    for PREV_LOCK_FILE in  /tmp/$NAME.lock.*
      do
        if [ -f $PREV_LOCK_FILE ]; then
          echo "Lockfile $PREV_LOCK_FILE from previous process exists"
          PREV_PID=$(echo $PREV_LOCK_FILE | cut -d'.' -f 3 )
          echo "Checking for process id $PREV_PID"
          if [ -e /proc/$PREV_PID ];then
            echo "Process of $PREV_PID is still running!"
            exit -1
          else
            echo "Process seems dead. Remove lock file"
            rm $PREV_LOCK_FILE
          fi
        fi
      done
    touch $LOCKFILE
  fi
}

function unlock() {
  if [ $# -ne 1 ];then
    "Process Name missing!"
    exit -1
  fi
  NAME=$1
  LOCKFILE=/tmp/$NAME.lock.$BASHPID
  rm $LOCKFILE
}

Then, in another script, include it at the beginning:

source /usr/local/include/lock_process.sh

Create a lock file:

lock "my_lock_name"

And remove it at the end:

unlock "my_lock_name"