-
Notifications
You must be signed in to change notification settings - Fork 0
/
runScript.sh
executable file
·280 lines (228 loc) · 7.6 KB
/
runScript.sh
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
# file: runScript.sh
#
#--------------------------------------------------------------------------
# This script will run automatically after startup, and make next schedule
# according to the "schedule.wpi" script file
#--------------------------------------------------------------------------
#-----------------------
# check if sudo is used
#-----------------------
if [ "$(id -u)" != 0 ]; then
echo 'Sorry, you need to run this script with sudo'
exit 1
fi
#---------------------------------
# delay if first argument exists
#---------------------------------
if [ ! -z "$1" ]; then
sleep $1
fi
#----------------------------------------------
# get current directory and schedule file path
#----------------------------------------------
cur_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
schedule_file="$cur_dir/schedule.wpi"
#----------------
# Read utilities
#----------------
. "$cur_dir/utilities.sh"
#--------------------------------------------
# pending until system time gets initialized
#--------------------------------------------
while [[ "$(date +%Y)" == *"1969"* ]] || [[ "$(date +%Y)" == *"1970"* ]]; do
sleep 1
done
#-----------------------
# get current timestamp
#-----------------------
cur_time=$(current_timestamp)
echo "--------------- $(date -d @$cur_time +'%Y-%m-%d %H:%M:%S') ---------------"
extract_timestamp()
while [[ "$(date +%Y)" == *"1969"* ]] || [[ "$(date +%Y)" == *"1970"* ]]; do
sleep 1
done
#-----------------------
# get current timestamp
#-----------------------
cur_time=$(current_timestamp)
echo "--------------- $(date -d @$cur_time +'%Y-%m-%d %H:%M:%S') ---------------"
extract_timestamp()
{
IFS=' ' read -r point date timestr <<< $1
local date_reg='(20[1-9][0-9])-([0-9][0-9])-([0-3][0-9])'
local time_reg='([0-2][0-9]):([0-5][0-9]):([0-5][0-9])'
if [[ $date =~ $date_reg ]] && [[ $timestr =~ $time_reg ]] ; then
echo $(date -d "$date $timestr" +%s)
else
echo 0
fi
}
extract_duration()
{
local duration=0
local day_reg='D([0-9]+)'
local hour_reg='H([0-9]+)'
local min_reg='M([0-9]+)'
local sec_reg='S([0-9]+)'
IFS=' ' read -a parts <<< $*
for part in "${parts[@]}"
do
if [[ $part =~ $day_reg ]] ; then
duration=$((duration+${BASH_REMATCH[1]}*86400))
elif [[ $part =~ $hour_reg ]] ; then
duration=$((duration+${BASH_REMATCH[1]}*3600))
elif [[ $part =~ $min_reg ]] ; then
duration=$((duration+${BASH_REMATCH[1]}*60))
elif [[ $part =~ $sec_reg ]] ; then
duration=$((duration+${BASH_REMATCH[1]}))
fi
done
echo $duration
}
setup_off_state()
{
log "Scheduled next startup at: $(date -d @$1 +'%Y-%m-%d %H:%M:%S')"
local date=$(date -d "@$1" +"%d")
local hour=$(date -d "@$1" +"%H")
local minute=$(date -d "@$1" +"%M")
local second=$(date -d "@$1" +"%S")
local when=$(get_utc_date_time $date $hour $minute $second)
IFS=' ' read -r date timestr <<< "$when"
IFS=':' read -r hour minute second <<< "$timestr"
set_startup_time $date $hour $minute $second
}
setup_on_state()
{
log "Scheduled next shutdown at: $(date -d @$1 +'%Y-%m-%d %H:%M:00')"
local date=$(date -d "@$1" +"%d")
local hour=$(date -d "@$1" +"%H")
local minute=$(date -d "@$1" +"%M")
local when=$(get_utc_date_time $date $hour $minute '00')
IFS=' ' read -r date timestr <<< "$when"
IFS=':' read -r hour minute second <<< "$timestr"
set_shutdown_time $date $hour $minute
}
if [ -f $schedule_file ]; then
begin=0
end=0
count=0
while IFS='' read -r line || [[ -n "$line" ]]; do
cpos=`expr index "$line" \#`
if [ $cpos != 0 ]; then
line=${line:0:$cpos-1}
fi
line=$(trim "$line")
if [[ $line == BEGIN* ]]; then
begin=$(extract_timestamp "$line")
elif [[ $line == END* ]]; then
end=$(extract_timestamp "$line")
elif [ "$line" != "" ]; then
states[$count]=$(echo $line)
count=$((count+1))
fi
done < $schedule_file
if [ $begin == 0 ] ; then
log 'Cannot find begin time in script'
elif [ $end == 0 ] ; then
log 'Cannot find end time in script'
elif [ $count == 0 ] ; then
log 'Cannot find any state defined in script.'
else
if [ $((cur_time < begin)) == '1' ] ; then
cur_time=$begin
fi
if [ $((cur_time >= end)) == '1' ] ; then
log 'Schedule script has ended already.'
else
schedule_script_interrupted
#-------------------------------------------------------------------------------
interrupted=$? # should be 0 if scheduled startup is in the future and shutdown is in the past
#-------------------------------------------------------------------------------
if [ ! -z "$2" ] && [ $interrupted == 0 ] ; then
log 'Schedule script interrupted; revising the schedule'
fi
index=0
found_states=0
check_time=$begin
script_duration=0
found_off=0
found_on=0
while [ $found_states != 2 ] && [ $((check_time < end)) == '1' ] ;
do
duration=$(extract_duration ${states[$index]})
check_time=$((check_time+duration))
if [ $found_off == 0 ] && [[ ${states[$index]} == OFF* ]] ; then
found_off=1
fi
if [ $found_on == 0 ] && [[ ${states[$index]} == ON* ]] ; then
found_on=1
fi
#--------------------------------------------------
# find the current ON state and incoming OFF state
#--------------------------------------------------
if [ $((check_time >= cur_time)) == '1' ] && ([ $found_states == 1 ] || [[ ${states[$index]} == ON* ]]) ; then
found_states=$((found_states+1))
if [[ ${states[$index]} == ON* ]]; then
if [[ ${states[$index]} == *WAIT ]]; then
log 'Skip scheduling next shutdown, which should be done externally.'
else
if [ ! -z "$2" ] && [ $interrupted == 0 ] ; then
#--------------------------------------------------
# schedule a shutdown 1 minute before next startup
#--------------------------------------------------
setup_on_state $((check_time-duration-60))
else
setup_on_state $check_time
fi
fi
elif [[ ${states[$index]} == OFF* ]] ; then
if [[ ${states[$index]} == *WAIT ]]; then
log 'Skip scheduling next startup, which should be done externally.'
else
if [ ! -z "$2" ] && [ $interrupted == 0 ] && [ $index != 0 ] ; then
#---------------------------------
# jump back to previous OFF state
#---------------------------------
prev_state=${states[$((index-1))]}
prev_duration=$(extract_duration $prev_state)
setup_off_state $((check_time-duration-prev_duration))
else
setup_off_state $check_time
fi
fi
else
log "Cannot recognize this state: ${states[$index]}"
fi
fi
index=$((index+1))
if [ $index == $count ] ; then
index=0
if [ $script_duration == 0 ] ; then
if [ $found_off == 0 ] ; then
log 'Need at least one OFF state in the script.'
#---------------------------
check_time=$end # skip all remaining cycles
#---------------------------
elif [ $found_on == 0 ] ; then
log 'Need at least one ON state in the script.'
#---------------------------
check_time=$end # skip all remaining cycles
#---------------------------
else
script_duration=$((check_time-begin))
skip=$((cur_time-check_time))
skip=$((skip-skip%script_duration))
#--------------------------
check_time=$((check_time+skip)) # skip some useless cycles
#--------------------------
fi
fi
fi
done
fi
fi
else
log "File \"schedule.wpi\" not found, skip running schedule script."
fi
echo '---------------------------------------------------'