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.
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.)
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