forked from awslabs/aws-iot-device-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreboot.sh
executable file
·57 lines (53 loc) · 1.41 KB
/
reboot.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
#!/usr/bin/env sh
set -e
echo "Running reboot.sh"
user=$1
echo "Username: $user"
GLOBAL_LAST_BOOT=""
REBOOT_LOCK_FILE=/var/tmp/dc-reboot.lock
get_last_boot_time() {
# Calculate exact time of last boot
UPTIME=$(cat /proc/uptime | head -n1 | awk '{print $1;}')
CURRENT=$(date "+%s")
GLOBAL_LAST_BOOT=$(awk -v currentTime=$CURRENT -v uptime=$UPTIME 'BEGIN {printf "%.0f",currentTime-uptime}')
}
if id "$user" 2>/dev/null && command -v "sudo" > /dev/null; then
if sudo -u "$user" -n test -f "$REBOOT_LOCK_FILE"; then
echo "Found lock file"
LOCK_FILE_BOOT=$(cat $REBOOT_LOCK_FILE)
rm $REBOOT_LOCK_FILE
get_last_boot_time
if [ "$LOCK_FILE_BOOT" -lt "$GLOBAL_LAST_BOOT" ]; then
echo "Reboot was successful"
exit 0
else
echo "Reboot failed"
exit 1
fi
else
echo "Did not find $REBOOT_LOCK_FILE"
date "+%s" > $REBOOT_LOCK_FILE
sudo -u "$user" -n reboot
fi
else
echo "username or sudo command not found"
if test -f "$REBOOT_LOCK_FILE"; then
echo "Found lock file"
LOCK_FILE_BOOT=$(cat $REBOOT_LOCK_FILE)
rm $REBOOT_LOCK_FILE
get_last_boot_time
if [ "$LOCK_FILE_BOOT" -lt "$GLOBAL_LAST_BOOT" ]; then
echo "Reboot was successful"
exit 0
else
echo "Reboot failed"
exit 1
fi
else
echo "Did not find $REBOOT_LOCK_FILE"
date "+%s" > $REBOOT_LOCK_FILE
reboot || true
rm $REBOOT_LOCK_FILE
exit 1
fi
fi