Semaphore - lockfile/flock

Hi,

I have a process which can run one instance at a time. Currently we have multiple scripts trying to kickoff this process. I wanted to implement the semaphore mechanism to achieve this.

I was going through few examples. The below code seems to be reasonable solution.

              ...
              lockfile important.lock
              ...
              access_"important"_to_your_hearts_content
              ...
              rm -f important.lock

But I am concerned about limitations, eg. we do not have much control on the filename (in the above case, "important")

Especially our team who maintains the application in Production are not experts.

Which technique (command) would you recommend (least errors and less experience to maintain)

I cannot see the problems / limitations that you quote. There are other lock mechanisms, but they are more sophisticated, so possibly less suitable for your team. What's the problem with the file name?

With any POSIX-conforming shell, you can also use:

set -C	# Set noclobber mode.  (Attempts to overwrite an existing file fail.)
if echo $$ > lockfile
then	printf 'Process ID %d now has the lock.\n' $$
else	read pid < lockfile
	printf 'Process ID %d holds the lock; exiting.\n' "$pid"
	exit 1
fi
set +C	# Return to default (no noclobber) mode.
trap 'rm -rf lockfile' EXIT	# Remove the lock when we exit.

This works to guarantee that only one process holds the lock (assuming that lockfile is an absolute pathname), but the read could fail if the lock was held at the time you requested the lock but the lock holder terminated before the read was processed. (So, you need to verify that the read worked rather than assume that it will always succeed.)

I suggest using the flock(1) command.

Or use mkdir

#!/bin/sh
trap "rmdir lockdir" EXIT
if mkdir lockdir
then
  echo start process
fi

This is backwards. If you do it this way, you will destroy a lock set by another process if you don't get the lock... You only want to set that trap after you acquire the lock.

#!/bin/sh
if mkdir lockdir
then
  trap "rmdir lockdir" EXIT
  echo start process
fi
1 Like