1 #!/bin/sh /etc/rc.common
5 PIDFILE="/tmp/run/watchcat"
8 local varname="$1"; local add="$2"; local separator="${3:- }"; local actual
9 eval "actual=\$$varname"
11 new="${actual:+$actual$separator}$add"
19 { [ "$time" -ge 1 ] 2> /dev/null && seconds="$time"; } || \
20 { [ "${time%s}" -ge 1 ] 2> /dev/null && seconds="${time%s}"; } || \
21 { [ "${time%m}" -ge 1 ] 2> /dev/null && seconds=$((${time%m}*60)); } || \
22 { [ "${time%h}" -ge 1 ] 2> /dev/null && seconds=$((${time%h}*3600)); } || \
23 { [ "${time%d}" -ge 1 ] 2> /dev/null && seconds=$((${time%d}*86400)); }
27 config_get period $1 period
28 config_get mode $1 mode "always"
29 config_get pinghosts $1 pinghosts "8.8.8.8"
30 config_get pingperiod $1 pingperiod
31 config_get forcedelay $1 forcedelay "0"
33 # Fix potential typo in mode (backward compatibility).
34 [ "$mode" = "allways" ] && mode="always"
38 timetoseconds "$period"
41 || append_string "error" 'period is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
42 [ "$mode" = "always" -o "$mode" = "ping" ] \
43 || append_string "error" "mode must be 'always' or 'ping'" "; "
44 [ -n "$pinghosts" -o "$mode" = "always" ] \
45 || append_string "error" "pinghosts must be set when in 'ping' mode" "; "
46 [ "$mode" = "ping" ] && {
47 if [ -n "$pingperiod" ]
49 timetoseconds "$pingperiod"
51 if [ "$pingperiod" -ge 0 ]
53 [ "$pingperiod" -lt "$period" ] \
54 || append_string "error" "pingperiod must be less than period" "; "
56 append_string "error" 'pingperiod is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
59 pingperiod="$((period/20))"
62 [ "$forcedelay" -ge 0 ] \
63 || append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; "
65 [ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
67 if [ "$mode" = "always" ]
69 /usr/bin/watchcat.sh "always" "$period" "$forcedelay" &
70 logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
72 /usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" &
73 logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)"
76 echo $! >> "${PIDFILE}.pids"
80 if [ -f "${PIDFILE}.pids" ]
82 logger -p user.info -t "watchcat" "stopping all tasks"
87 done < "${PIDFILE}.pids"
91 logger -p user.info -t "watchcat" "all tasks stopped"
93 logger -p user.info -t "watchcat" "no tasks running"
98 [ -f "${PIDFILE}.pids" ] && stop
101 if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
103 logger -p user.info -t "watchcat" "starting all tasks"
104 config_foreach load_watchcat watchcat
105 logger -p user.info -t "watchcat" "all tasks started"
107 logger -p user.info -t "watchcat" "no tasks defined"