Created
November 29, 2011 18:49
-
-
Save gyuchang/1405920 to your computer and use it in GitHub Desktop.
Achieving local mutex via a lock file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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