forked from markjdb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollcond
executable file
·79 lines (68 loc) · 1.81 KB
/
pollcond
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
#!/bin/sh
usage()
{
cat >&2 <<__EOF__
Usage: $(basename $0) [-l] [-m <addr>] -a <action -c <condition> -t <interval>
Options:
-a <action> -- The action to be executed when the condition
-c <condition> -- The condition to be periodically evaluated.
-l -- If specified, the script will continue to run
after the condition has been detected to be true.
Otherwise the script will exit with a status of 0.
-m <addr> -- Send an email to <addr> when the condition becomes
true.
-t <interval> -- Sleep for <interval> seconds in between each check.
Use an interval of 0 to busy-wait on the condition.
__EOF__
exit ${1-1}
}
log()
{
echo "$(basename $0): $1" >&2
}
INFINITE=false
while [ $# -gt 0 ]; do
case $1 in
-a)
shift; ACTION=$1
;;
-c)
shift; COND=$1
;;
-l)
INFINITE=true
;;
-m)
shift; MADDR=$1
;;
-t)
shift; INTERVAL=$1
;;
*)
usage
;;
esac
shift
done
if [ -z "$ACTION" -o -z "$COND" -o -z "$INTERVAL" ]; then
usage
elif ! [ $(expr "$INTERVAL" : [1-9][0-9]*) -eq ${#INTERVAL} ]; then
log "Invalid interval \"${INTERVAL}\""
usage
fi
while true ; do
eval "$COND" >/dev/null 2>&1
if [ $? -eq 0 ]; then
if [ -n "$MADDR" ]; then
mail -s pollcond "$MADDR" <<__EOF__
$(basename $0): condition "$COND" evaluated to true. Executing "$ACTION":
$(eval "$ACTION")
__EOF__
else
log "condition \"$COND\" evaluated to true. Executing \"$ACTION\":"
eval "$ACTION"
fi
[ $INFINITE = false ] && exit 0
fi
[ $INTERVAL -gt 0 ] && sleep $INTERVAL
done