-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions
91 lines (76 loc) · 1.75 KB
/
functions
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
86
87
88
89
90
91
function print_msg() {
local msg="$1"
if [ "$PRINT_TO_TTY1" == "yes" ] ; then
echo "$msg" >${PRINT_TTY:-/dev/tty1}
else
echo "$msg"
fi
}
function register() {
local __ret_val="0"
if [ -z "${1:-}" ] ; then
touch "$SERVICE_RUN_DIR/$service_name"
__ret_val=$?
elif [[ "$1" =~ ^[0-9]+$ ]] ; then
echo "$1" > "$SERVICE_RUN_DIR/$service_name"
__ret_val=$?
else
local __pid="`pidof "$1"`"
local i=0
while [ "$i" -lt "$TRY" -a -z "$__pid" ] ; do
sleep $TIMEOUT
__pid="`pidof "$1"`"
__ret_val="$?"
i=$[i+1]
done
if [ -n "$__pid" ] ; then
print_msg "Found $__pid of $1 of $service_name"
register "$__pid"
else
__ret_val="-124"
print_msg "No process $1 found"
fi
fi
return $__ret_val
}
function unregister() {
rm "$SERVICE_RUN_DIR/$service_name" 2>/dev/null
local __ret_val=$?
if [ "$__ret_val" != "0" ] ; then
print_msg "PID file $SERVICE_RUN_DIR/$service_name not found";
fi
return $__ret_val
}
function success() {
ret_val="${1:-0}"
return "$ret_val"
}
function check_process_running() {
local __pid="$1"
kill -s 0 "$__pid" 2>/dev/null
local __ret_val="$?"
local i="0"
while [ "$i" -lt "$TRY" -a "$__ret_val" == "0" ] ; do
sleep $TIMEOUT
kill -s 0 "$__pid" 2>/dev/null
__ret_val="$?"
i=$[i+1]
done
return $__ret_val
}
function try_stop_process() {
local __signal="$1"
local __pid="$2"
kill -s "$__signal" "$__pid" 2>/dev/null
kill -s 0 "$__pid" 2>/dev/null
local __ret_val="$?"
local i="0"
while [ "$i" -lt "$TRY" -a "$__ret_val" == "0" ] ; do
sleep $TIMEOUT
kill -s "$__signal" "$__pid" 2>/dev/null
kill -s 0 "$__pid" 2>/dev/null
__ret_val="$?"
i=$[i+1]
done
return $__ret_val
}