-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathsys-wait
executable file
·77 lines (63 loc) · 1.9 KB
/
sys-wait
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
#!/bin/bash
set -eo pipefail
usage() {
local bin=$(basename $0)
[[ $1 -eq 0 ]] || exec 1>&2
echo "Usage: $bin$(gawk \
'/^\s+case\>/ {parse=1; m=0; next}
/^\s+esac\>/ {exit}
!parse {next}
match($0, /^\s*([\-|a-zA-Z0-9]+)\)/, a) {
if (ac==0 || ac<2) ac=""
else ac= ac==2 ? " val" : sprintf(" val[x%s]", ac-1)
if (ap) printf( "%s[ %s%s ]", m>2 ? "\n " : " ", ap, ac)
ap=a[1]; ac=0; m++ }
{for (i=1;i<NF;i++) if (match($i, /\<shift\>/)) ac++}' $0)"
echo
echo "Wait until all specified system/load conditions are met."
echo "E.g. load average value below specified threshold (--load* options)."
echo
exit ${1:-1};
}
interval=60
load1= load5= load15=
file= file_nx=
while [[ -n "$1" ]]; do
case "$1" in
-h|--help) usage 0 ;;
-x|--debug) shift; set -x ;;
-i|--check-interval) shift; interval=$1; shift ;;
-l|--load1) shift; load1=$1; shift ;;
--load5) shift; load5=$1; shift ;;
--load15) shift; load15=$1; shift ;;
-f|--file) shift; file=$1; shift ;;
-F|--file-nx) shift; file_nx=$1; shift ;;
welp) exit 1 ;;
*) echo "ERROR: Unknown option - $1"; usage 1 ;;
esac
done
load_parse() {
[[ "$1" =~ ^([0-9]+)(\.([0-9]+))?$ ]] || return 1
local n=${BASH_REMATCH[1]}
local m=${BASH_REMATCH[3]:0:2}
[[ -n "$m" ]] || m=00
n="$n$m"
while [[ ${n:0:1} = 0 ]]; do n=${n:1}; done
[[ -n "$n" ]] || n=0
echo "$n"; }
load_test() {
local value=$(load_parse "$1")
local thresh=$(load_parse "$2")
[[ "$value" -le "$thresh" ]] || return 1; }
while :; do
err=0
[[ -z "$load1" && -z "$load5" && -z "$load15" ]] || {
read m1 m5 m15 rest < /proc/loadavg
[[ -z "$load1" ]] || load_test "$m1" "$load1" || err=1
[[ -z "$load5" ]] || load_test "$m5" "$load5" || err=1
[[ -z "$load15" ]] || load_test "$m15" "$load15" || err=1; }
[[ -z "$file" ]] || { [[ -e "$file" ]] || err=1; }
[[ -z "$file_nx" ]] || { [[ ! -e "$file_nx" ]] || err=1; }
[[ $err -ne 0 ]] || break
sleep $interval
done