This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathautosuspend.sh
executable file
·165 lines (149 loc) · 4.53 KB
/
autosuspend.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
#!/bin/bash
. /etc/autosuspend
. /usr/local/sbin/autosuspend.tvheadend-functions
logit()
{
logger -p local0.notice -i -t autosuspend -- $*
return 0
}
IsOnline()
{
for i in $*; do
ping $i -c1
if [ "$?" == "0" ]; then
logit "PC $i is still active, auto suspend terminated"
return 1
fi
done
return 0
}
IsRunning()
{
for i in $*; do
if [ `pgrep -c $i` -gt 0 ] ; then
logit "$i still active, auto suspend terminated"
return 1
fi
done
return 0
}
IsDaemonActive()
{
for i in $*; do
if [ `pgrep -c $i` -gt 1 ] ; then
logit "$i still active, auto suspend terminated"
return 1
fi
done
return 0
}
IsBusy()
{
# Samba
if [ "x$SAMBANETWORK" != "x" ]; then
samba_status=$(/usr/bin/smbstatus -b)
if [ $(echo $samba_status | grep $SAMBANETWORK | wc -l) != "0" ]; then
logit "Connected samba clients: $samba_status"
logit "samba connected, auto suspend terminated"
return 1
fi
fi
#daemons that always have one process running
IsDaemonActive $DAEMONS
if [ "$?" == "1" ]; then
return 1
fi
#backuppc, wget, wsus, ....
IsRunning $APPLICATIONS
if [ "$?" == "1" ]; then
return 1
fi
# Read logged users
USERCOUNT=`who | wc -l`;
# No Suspend if there are any users logged in
test $USERCOUNT -gt 0 && { logit "some users still connected, auto suspend terminated"; return 1; }
IsOnline $CLIENTS
if [ "$?" == "1" ]; then
return 1
fi
# Tvheadend
IsTvheadendBusy
if [ "$?" == "1" ]; then
return 1
fi
return 0
}
COUNTFILE=/var/spool/suspend_counter
OFFFILE=/var/spool/suspend_off
# turns off the auto suspend
if [ -e "$OFFFILE" ]; then
logit "auto suspend is turned off by existence of $OFFFILE"
exit 0
fi
if [ "$AUTO_SUSPEND" = "true" ] || [ "$AUTO_SUSPEND" = "yes" ] ; then
IsBusy
if [ "$?" == "0" ]; then
# was it not busy already last time? Then suspend.
if [ -e "$COUNTFILE" ]; then
# only auto-suspend at night
if [ \( "$DONT_SUSPEND_BY_DAY" != "true" -a "$DONT_SUSPEND_BY_DAY" != "yes" \) -o \( "`date +%H`" -ge "3" -a "`date +%H`" -lt "8" \) ]; then
# notice resume-plan
NEXTWAKE="0"
if [ -e /etc/autosuspend_resumeplan ]; then
while read line; do
if [ "`date +%s -d \"$line\"`" -gt "`date +%s`" -a \( "`date +%s -d \"$line\"`" -lt "$NEXTWAKE" -o "$NEXTWAKE" = "0" \) ]; then
NEXTWAKE="`date +%s -d \"$line\"`"
fi
done < /etc/autosuspend_resumeplan
fi
if [ "$NEXTWAKE" -gt "`date +%s`" ]; then
if [ "$NEXTWAKE" -gt "`expr \"\`date +%s\`\" + 1800`" ]; then
echo "0" > /sys/class/rtc/rtc0/wakealarm
echo "$NEXTWAKE" > /sys/class/rtc/rtc0/wakealarm
logit "will resume at $NEXTWAKE"
else
logit "do not suspend because would have been awaken within next 30 minutes"
exit 0
fi
fi
# and suspend or reboot:
rm -f "$COUNTFILE"
if [ "$REBOOT_ONCE_PER_WEEK" = "true" -o "$REBOOT_ONCE_PER_WEEK" = "yes" ] && [ "`echo \"scale=2; ( \`cat /proc/uptime | cut -d' ' -f1-1\` / 3600 / 24 ) >= 7\" | bc`" -gt 0 ]; then
logit "REBOOTING THE MACHINE BECAUSE IT HAS BEEN RUNNING FOR MORE THAN A WEEK"
shutdown -r now
else
logit "AUTO SUSPEND CAUSED"
suspend_method=${SUSPEND_METHOD:-hibernate}
logit "Suspend method: $suspend_method"
SetWakeupTime
case "$suspend_method" in
"suspend") systemctl suspend
;;
"hibernate") systemctl hibernate
;;
"hybrid-sleep") systemctl hybrid-sleep
;;
"poweroff") systemctl poweroff
;;
*) logit "Aborting because of unsupported suspend method: $suspend_method"
;;
esac
fi
else
logit "did not auto suspend because it is broad day"
fi
exit 0
else
# shut down next time
touch "$COUNTFILE"
logit "marked for suspend in next try"
exit 0
fi
else
rm -f "$COUNTFILE"
logit "aborted"
exit 0
fi
fi
logit "malfunction"
exit 1