-
Notifications
You must be signed in to change notification settings - Fork 1
/
lock.sh
51 lines (44 loc) · 1.04 KB
/
lock.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function getlock()
{
local LBASE TEMPFILE LOCKFILE
LBASE="$1"
if [ -z "$LBASE" ]; then
error "Parameter 'lbase' not specified in function 'getlock()'."
fi
TEMPFILE="$LBASE.$$"
LOCKFILE="$LBASE.lock"
# write pid and try to get lock soft-linking pid-file
( echo "$$" > "$TEMPFILE" ) >& /dev/null || {
error "You don't have permission to access `dirname "$TEMPFILE"`"
return 1
}
ln "$TEMPFILE" "$LOCKFILE" >& /dev/null && {
# oh yeah! i got it!
rm -f "$TEMPFILE"
return 0
}
# i couldn't lock that file... check if process that created it exists
kill -0 `cat "$LOCKFILE"` >& /dev/null && {
rm -f "$TEMPFILE"
return 1
}
warning "Removing stale lock file"
rm -f "$LOCKFILE"
# try to get lock...
ln "$TEMPFILE" "$LOCKFILE" >& /dev/null && {
rm -f "$TEMPFILE"
return 0
}
# sht
rm -f "$TEMPFILE"
return 1
}
function releaselock()
{
local LBASE
LBASE="$1"
if [ -z "$LBASE" ]; then
error "Parameter 'lbase' not specified in function 'releaselock()'."
fi
rm -f "$LBASE.lock"
}