-
Notifications
You must be signed in to change notification settings - Fork 18
/
rc.func.AdGuardHome
113 lines (105 loc) · 3.12 KB
/
rc.func.AdGuardHome
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/sh
ACTION="$1"
CALLER="${2:-$0}"
ansi_red="$(printf "\033[1;31m")"
ansi_white="$(printf "\033[1;37m")"
ansi_green="$(printf "\033[1;32m")"
ansi_yellow="$(printf "\033[1;33m")"
ansi_blue="$(printf "\033[1;34m")"
ansi_bell="$(printf "\007")"
ansi_blink="$(printf "\033[5m")"
ansi_std="$(printf "\033[m")"
ansi_rev="$(printf "\033[7m")"
ansi_ul="$(printf "\033[4m")"
start() {
{ [ "${CRITICAL}" != "yes" ] && [ "${CALLER}" = "cron" ]; } && return 7
[ "${ENABLED}" != "yes" ] && return 8
printf "%s" "${ansi_white} Starting ${DESC}... ${ansi_std}"
if [ -n "$(pidof "${PROC}")" ]; then
printf "%s\n" " ${ansi_yellow} already running. ${ansi_std}"
return 0
fi
if [ "${PROC}" != "AdGuardHome" ] || ! grep -q '^port=553' /etc/dnsmasq.conf; then { ${PRECMD} >/dev/null 2>&1; }; fi
{ ${PREARGS} ${PROC} ${ARGS} >/dev/null 2>&1 </dev/null & }
#echo ${PREARGS} ${PROC} ${ARGS}
local COUNTER="0" LIMIT="10"
while { [ -z "$(pidof "${PROC}")" ] && [ "${COUNTER}" -le "${LIMIT}" ]; }; do
sleep 1s
COUNTER="$((COUNTER + 1))"
done
if [ "${PROC}" != "AdGuardHome" ] || ! grep -q '^port=553' /etc/dnsmasq.conf; then { ${POSTCMD} >/dev/null 2>&1; }; fi
if [ -z "$(pidof "${PROC}")" ]; then
printf "%s\n" " ${ansi_red} failed. ${ansi_std}"
logger -st "${CALLER}" "Failed to start ${DESC} from ${CALLER}."
return 255
else
{ printf "%s\n" " ${ansi_green} done. ${ansi_std}"; }
logger -st "${CALLER}" "Started ${DESC} from ${CALLER}."
return 0
fi
}
stop() {
case "${ACTION}" in
"stop" | "restart")
local COUNTER="0" LIMIT="10"
while { [ -n "$(pidof "${PROC}")" ] && [ "${COUNTER}" -le "${LIMIT}" ]; }; do
[ "${COUNTER}" = "0" ] && printf "%s" "${ansi_white} Shutting down ${PROC}... ${ansi_std}"
[ -z "$(pidof "${PROC}")" ] && break || { kill -s 2 "$(pidof "${PROC}")" 2>/dev/null || killall -q -2 "${PROC}" 2>/dev/null; }
sleep "${LIMIT}"s
COUNTER="$((COUNTER + 1))"
done
;;
"kill")
printf "%s" "${ansi_white} Killing ${PROC}... ${ansi_std}"
{ kill -s 2 "$(pidof "${PROC}")" 2>/dev/null || killall -q -2 "${PROC}" 2>/dev/null; }
;;
esac
if [ -n "$(pidof "${PROC}")" ]; then
printf "%s\n" " ${ansi_red} failed. ${ansi_std}"
return 255
else
printf "%s\n" " ${ansi_green} done. ${ansi_std}"
return 0
fi
}
check() {
printf "%s" "${ansi_white} Checking ${DESC}... "
if [ -n "$(pidof "${PROC}")" ]; then
printf "%s\n" " ${ansi_green} alive. ${ansi_std}"
return 0
else
printf "%s\n" " ${ansi_red} dead. ${ansi_std}"
return 1
fi
}
reload() {
SIGNAL="HUP"
printf "%s\n" "${ansi_white} Sending ${SIGNAL} to ${PROC}... ${ansi_std}"
{ kill -s "${SIGNAL}" "$(pidof "${PROC}")" 2>/dev/null || killall -"${SIGNAL}" "${PROC}" 2>/dev/null; }
}
for PROC in ${PROCS}; do
DESC="${DESC:-$PROC}"
case "${ACTION}" in
"start")
start
;;
"stop" | "kill")
check && stop
;;
"restart")
{ check >/dev/null; } && stop
start
;;
"check")
check
;;
"reload")
{ check >/dev/null; } && reload
;;
*)
printf "%s\n" "${ansi_white} Usage: $0 (start|stop|restart|check|kill|reload)${ansi_std}"
exit 1
;;
esac
done
#logger "Leaving ${0##*/}."