Skip to content

Instantly share code, notes, and snippets.

@gyuchang
Created November 29, 2011 18:49
Show Gist options
  • Select an option

  • Save gyuchang/1405920 to your computer and use it in GitHub Desktop.

Select an option

Save gyuchang/1405920 to your computer and use it in GitHub Desktop.
Achieving local mutex via a lock file
#!/bin/bash
exec 0>&- # close stdin
exec 1>&- # close stdout - comment out this line to see debug message
exec 2>&- # close stderr
base='/tmp'
list=`find $base -type f -name '*.lock' -print 2>/dev/null`
for lockfile in $list; do
# see if any process is opening this file
`/usr/sbin/lsof $lockfile 2>/dev/null 1>/dev/null`
if [[ "$?" -gt 0 ]]; then
# delete
echo "$lockfile is not in use, attempting to delete"
`rm -r $lockfile`
if [[ "$?" -eq 0 ]]; then
echo "$lockfile deleted"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment