-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstayawake.sh
executable file
·51 lines (46 loc) · 1.5 KB
/
stayawake.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
#!/bin/bash
# If the output of this function changes between two successive runs of this
# script, we inhibit auto-suspend.
function check_activity()
{
/usr/sbin/nfsstat -c -3
}
# Prevent the automatic suspend from kicking in.
function inhibit_suspend()
{
# Slightly jiggle the mouse pointer about; we do a small step and
# reverse step to try to stop this being annoying to anyone using the
# PC. TODO: This isn't ideal, apart from being a bit hacky it stops
# the screensaver kicking in as well, when all we want is to stop
# the PC suspending. Can 'caffeine' help?
export DISPLAY=:0.0
xdotool mousemove_relative --sync -- 10 10
xdotool mousemove_relative --sync -- -10 -10
}
CWD=`dirname $(realpath $0)`
LOG="$CWD/stayawake.log"
ACTIVITYFILE1="$CWD/stayawake.1"
ACTIVITYFILE2="$CWD/stayawake.2"
TMP=$(tail -n 197 $LOG 2>/dev/null) && echo "${TMP}" > $LOG
echo "" >> $LOG
echo "Started run at $(date)" >> $LOG
if [ $(ss state established '( sport = 22 or sport = 445 )' | wc -l) -gt 1 ]; then
echo "SSH and/or Samba session(s) active, inhibiting suspend." >> $LOG
inhibit_suspend
exit 1;
else
if [ ! -f "$ACTIVITYFILE1" ]; then
check_activity > "$ACTIVITYFILE1"
exit 0;
fi
/bin/mv "$ACTIVITYFILE1" "$ACTIVITYFILE2"
check_activity > "$ACTIVITYFILE1"
if cmp --quiet "$ACTIVITYFILE1" "$ACTIVITYFILE2"; then
echo "No activity detected since last run" >> $LOG
else
echo "Activity detected since last run; preventing suspend." >> $LOG
inhibit_suspend
exit 1;
fi
exit 0;
fi