-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilelock
executable file
·53 lines (45 loc) · 1.04 KB
/
filelock
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
#!/bin/bash
retries="10"
action="lock"
while getopts "lur:" opt
do
case $opt in
l ) action="lock" ;;
u ) action="unlock" ;;
r ) retries="$OPTARG" ;;
? ) echo "$(basename $0): Unrecognize parameters"; exit 1 ;;
esac
done
shift $((OPTIND - 1))
#Output a multiline message to stdout
if [ $# -eq 0 ]
then
cat << EOF >&2
Usage: $(basename $0) [-l][-u] [-r retries] LOCKFILE
Where -l requests a lock (the default), -u requests an unlock, -r X
specifies a max number of retries before it fails (default = $retries).
EOF
exit 1
fi
#Ascertain if we have the lockfile command.
if [ -z "$(which lockfile | grep -v 'not found')" ]
then
echo "$(basename $0) failed: 'lockfile' utility not found in PATH." >&2
exit 1
fi
if [ "$action" = "lock" ]
then
if ! lockfile -1 -r $retries "$1" 2> /dev/null
then
echo "$(basename $0): Failed: Couldn't create lockfile in time." >&2
exit 1
fi
else
if [ ! -f "$1" ]
then
echo "$(basename $0): Warning: lockfile $1 doesn't exist to unlock." >&2
exit 1
fi
rm -f "$1"
fi
exit 0