-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnc_lock-check.sh
82 lines (49 loc) · 1.87 KB
/
fnc_lock-check.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
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
#
# checks for existence of lockfile for execution of current script with current *.cfg file
# exits with 1 if lockfile exist and pid that created it is still running
# else removes stale lock files, creates new lock files, returns 0 and continues
pid=$$
script="$(basename $0)"
config_file="$(basename $1)"
config_path="$(readlink -f $1)"
lock_file="$lock_dir/$script/$config_file.lock"
pid_file="$lock_dir/$script/$config_file.pid"
do_lock_check() {
printf "\n--------------------------------------( do_lock_check )-----------------------------------------\n" 1>&4
# lock files creation and lock checks
[ -d "$lock_dir" ] || mkdir -p "$lock_dir"
[ -d "$lock_dir/$script" ] || mkdir "$lock_dir/$script"
if [ -f $lock_file ] ;then
local config_pid="$(cat $pid_file)"
local config_pid_comm="$(ps -p $config_pid -o comm=)"
ps -p "$config_pid" > /dev/null 2>&1
if [ "$?" = 0 ] && [ "$config_pid_comm" = "$script" ] ;then
echo "[ERROR] lock files exist for $script $config_file and script is running , exit 1" 1>&3
exit 1
else
echo "[info2] lock files exist for $script $config_file but are stale , removing" 1>&4
rm "$lock_file"
rm "$pid_file"
fi
fi
if [ -w "$lock_dir/$script" ] ;then
echo "[info2] creating lock_file for $script $config_file" 1>&4
echo "[info2] creating pid_file for $script $config_file" 1>&4
echo "$config_path" > "$lock_file"
echo "$pid" > "$pid_file"
else
echo "[ERROR] unable to create lock files in $lock_dir/$script , locking functionality diabled" 1>&3
fi
return 0
}
do_lock_clear() {
printf "\n--------------------------------------( do_lock_clear )-----------------------------------------\n" 1>&4
# lock files remove
if [ -f $lock_file ] ;then
echo "[info2] removing lock_file for $script $config_file" 1>&4
echo "[info2] removing pid_file for $script $config_file" 1>&4
rm "$lock_file"
rm "$pid_file"
fi
return 0
}