This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
deviceRebootCronscheduler.sh
125 lines (102 loc) · 4.08 KB
/
deviceRebootCronscheduler.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
#! /bin/sh
source /etc/log_timestamp.sh
source /usr/ccsp/tad/corrective_action.sh
CRONTAB_DIR="/var/spool/cron/crontabs/"
CRONTAB_FILE=$CRONTAB_DIR"root"
CRONFILE_BK="/tmp/cron_tab$$.txt"
LOG_FILE="$1"
REBOOT_SCRIPT="$2"
FW_START="/nvram/.FirmwareUpgradeStartTime"
FW_END="/nvram/.FirmwareUpgradeEndTime"
#Pending reboot flags
XPKI_CERT_UPDATE_REBOOT_SCHEDULED="/tmp/.XpkiCrtUpdtdWaitingReboot"
RFC_REBOOT_SCHEDULED="/tmp/.RfcwaitingReboot"
DOWNLOAD_INPROGRESS="/tmp/.downloadingfw"
REBOOT_WAIT="/tmp/.waitingreboot"
if [ -f /etc/device.properties ]
then
source /etc/device.properties
fi
if [ -z $LOG_FILE ]
then
echo_t "Error!!! log file not passed to deviceRebootCronscheduler.sh"
exit 1
fi
if [ -z $REBOOT_SCRIPT ] || [ ! -f $REBOOT_SCRIPT ]
then
echo_t "[deviceRebootCronscheduler.sh] Error!!! Reboot Script not set" >> $LOG_FILE
exit 1
fi
calcRebootExecTime()
{
# Extract maintenance window start and end time
if [ -f "$FW_START" ] && [ -f "$FW_END" ]
then
start_time=`cat $FW_START`
end_time=`cat $FW_END`
else
start_time=3600
end_time=14400
fi
#if start_time and end_time are equal, set them to default
if [ "$start_time" = "$end_time" ]
then
echo_t "[deviceRebootCronscheduler.sh] start_time and end_time are equal.so,setting them to default" >> $LOG_FILE
start_time=3600
end_time=14400
fi
#Get local time off set
time_offset=`dmcli eRT getv Device.Time.TimeOffset | grep "value:" | cut -d ":" -f 3 | tr -d ' '`
#Maintence start and end time in local
if [ "x$BOX_TYPE" = "xHUB4" ]
then
main_start_time=$start_time
main_end_time=$end_time
else
main_start_time=$((start_time-time_offset))
main_end_time=$((end_time-time_offset))
fi
#calculate random time in sec
rand_time_in_sec=`awk -v min=$main_start_time -v max=$main_end_time -v seed="$(date +%N)" 'BEGIN{srand(seed);print int(min+rand()*(max-min+1))}'`
# To avoid cron to be set beyond 24 hr clock limit
if [ $rand_time_in_sec -ge 86400 ]
then
rand_time_in_sec=$((rand_time_in_sec-86400))
echo_t "[deviceRebootCronscheduler.sh] Random time in sec exceed 24 hr limit.setting it correct limit" >> $LOG_FILE
fi
#conversion of random generated time to HH:MM:SS format
#calculate random second
rand_time=$rand_time_in_sec
rand_sec=$((rand_time%60))
#calculate random minute
rand_time=$((rand_time/60))
rand_min=$((rand_time%60))
#calculate random hour
rand_time=$((rand_time/60))
rand_hr=$((rand_time%60))
echo_t "[deviceRebootCronscheduler.sh] start_time: $start_time, end_time: $end_time" >> $LOG_FILE
echo_t "[deviceRebootCronscheduler.sh] time_offset: $time_offset" >> $LOG_FILE
echo_t "[deviceRebootCronscheduler.sh] main_start_time: $main_start_time , main_end_time= $main_end_time" >> $LOG_FILE
echo_t "[deviceRebootCronscheduler.sh] rand_time_in_sec: $rand_time_in_sec ,rand_hr: $rand_hr ,rand_min: $rand_min ,rand_sec: $rand_sec" >> $LOG_FILE
}
ScheduleCron()
{
# Dump existing cron jobs to a file & add new job
crontab -l -c $CRONTAB_DIR > $CRONFILE_BK
echo "$rand_min $rand_hr * * * $REBOOT_SCRIPT" >> $CRONFILE_BK
crontab $CRONFILE_BK -c $CRONTAB_DIR
rm -rf $CRONFILE_BK
touch $XPKI_CERT_UPDATE_REBOOT_SCHEDULED
}
if [ -f $XPKI_CERT_UPDATE_REBOOT_SCHEDULED ] || [ -f $RFC_REBOOT_SCHEDULED ] || [ -f $DOWNLOAD_INPROGRESS ] || [ -f $REBOOT_WAIT ]
then
echo_t "[deviceRebootCronscheduler.sh] Abort!!!, reboot schedule in progress" >> $LOG_FILE
else
#calculate and schedule cron job
calcRebootExecTime
if [ -f $CRONTAB_FILE ]
then
ScheduleCron
echo_t "[deviceRebootCronscheduler.sh] Reboot cron job scheduled with $REBOOT_SCRIPT" >> $LOG_FILE
fi
fi