forked from awein/SleepProxyClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckSleep.sh
More file actions
88 lines (72 loc) · 1.71 KB
/
checkSleep.sh
File metadata and controls
88 lines (72 loc) · 1.71 KB
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
#!/bin/bash
export PATH=/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export LANG=en_US.utf8
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# checkSleep
#
# execute SleepProxyClient and pm-suspend if two runs after each other are positive
#
# Creteria:
# - no user logged in (remote + local)
# - no non-local active tcp connections
#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#used to check for a previous successfull run
TMPFILE="/tmp/checkSleep"
#value will be returned. 0 if the creteria was fullfilled
RET=0
# network interface to use
IFACE="eth0"
SCRIPT_DIR=$(dirname $0)
# run SleepProxyClient
function doSleep {
logger "checkSleep: initiating sleep"
pm-suspend
logger "checkSleep: awake!"
}
# check the creteria
function doCheck {
RESULT=0
# check for logged in user
USERS=`who | wc -l`
if [ $USERS -gt 0 ]
then
# echo "Active users: $USERS"
RESULT=1
fi
# check if no non-local connection is active
CONNS=`netstat -tn | grep -v "127.0.0.1" | grep "ESTABLISHED" | wc -l`
if [ $CONNS -gt 0 ]
then
# echo "Active connections: $CONNS"
RESULT=1
fi
#check for heavy processing/cpu load,
LOAD5MINAVG=`cat /proc/loadavg | cut -d " " -f 2`
if [ `echo "$LOAD5MINAVG > 1" | bc` -gt 0 ]
then
RESULT=1
fi
return $RESULT
}
doCheck
if [ $? -eq 0 ]
then
# we only want to go to sleep if two successive doCheck runs were successfull
# check whether the previous run created the file to signal success
if [ -e "$TMPFILE" ]
then
# cleanup
rm -f "$TMPFILE"
# initiate sleep
doSleep
else
# mark run as positive
touch "$TMPFILE"
fi
else
rm -f "$TMPFILE"
RET=1
fi
exit $RET