-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuglylock
executable file
·85 lines (72 loc) · 1.48 KB
/
uglylock
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env sh
# $1 lock file
function set_lockfile()
{
set -o noclobber
printf $PPID > "$1" 2>/dev/null && return 0
locked_pid=$(< "$1")
[ $locked_pid -gt "0" ] || return 1
kill -0 $locked_pid 2>/dev/null && return 1
rm "$1" 2>/dev/null
printf "$PPID" > "$1" 2>/dev/null && return 0
return 1
}
# $1 timeout
# $2 lock file
function set_lockfile_wait()
{
declare -ir start_time=$(date '+%s')
declare -ir timeout="${1:-0}"
while ! set_lockfile "$2" 2>/dev/null
do
if [ $(($(date '+%s') - $start_time)) -ge $timeout ] ; then
printf "Error: waited too long for lock file %s\n" "$2" 1>&2
return 1
fi
sleep 1
done
return 0
}
# $1 lock file
function release_lockfile()
{
[ -e "$1" ] && grep -Fxq $PPID "$1" && rm -f "$1"
}
function print_usage()
{
printf "%s [-h][-t n][-u] <lock file>\n" $(basename "$0")
}
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
print_usage
exit 0
;;
-t|--timeout)
declare -ir TIMEOUT=$2
shift
shift
;;
-u|--unlock)
UNLOCK=true
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
if [ "$UNLOCK" = true ]
then
release_lockfile "$1"
exit 0
fi
if ! set_lockfile_wait ${TIMEOUT:-0} "$1" ; then
exit 1
fi
exit 0