aboutsummaryrefslogtreecommitdiff
path: root/examples/var_service/dhcp_if_pinger/run
blob: 25341243fbeae124fd46224ef59af6d2d7a42f6b (plain)
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
#!/bin/sh

# How often to test, seconds
ping_time=67
# "One ping, must have reply in 1 sec"
ping_opts="-c1 -W1 -w1"
# If ping failed, how soon to retry
retry_time=5
# Reinit after this many consecutive ping error
max_fail=5
# Interface whose DHCP data to use
if=${PWD##*/dhcp_}
if=${if%%_pinger}

msg() {
	echo "`date '+%Y-%m-%d %H:%M:%S'` $*" >>"$0.log"
}

if test -f "$0.log"; then
	tail -999 "$0.log" >"$0.log.new"
	mv "$0.log.new" "$0.log"
fi

test -f "../dhcp_$if/env.out" || exec env - sleep "$ping_time"

. "../dhcp_$if/env.out"
test x"$router" != x"" || exec env - sleep "$ping_time"

#msg "Pinging $router"
failcnt=0
while true; do
	ping $ping_opts "$router" && exec env - sleep "$ping_time"
	failcnt=$((failcnt+1))
	msg "Failed to ping $router, fail count:$failcnt"
	test $failcnt -ge $max_fail && break
	env - sleep "$retry_time"
done

test -d "../dhcp_$if" && {
	msg "Restarting dhcp_$if"
	svc -t "dhcp_$if"
}
test -d "../supplicant_$if" && {
	msg "Restarting supplicant_$if"
	svc -t "supplicant_$if"
}
exec env - sleep "$ping_time"