-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep-if-idle.py
executable file
·114 lines (89 loc) · 2.87 KB
/
sleep-if-idle.py
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
#!/usr/bin/python
'''
If you run Arch, it doesn't come w/ a suspend timer by default. Oops.
However, since it's Lunix we can do a bit better
Requires:
pacaur -S xautolock
Put this in ~/.config/openbox/autostart
xautolock -detectsleep -time 10 -locker /home/lhl/bin/sleep-if-idle.py -notify 60 -notifier 'notify-send -u critical -t 10000 "SLEEP in 60s"' &
This script will:
* IGNORED: do an AC check
* Check if sound is in use (presumably video watching will trigger this and it's slightly cleaner than looking for full screen apps)
* Look for specific processes running: rsync, scp, cp, mv
* Look for file read/write greater than 2MB - should cover nonslow writes w/ Thunar and anything else missed
See also: http://rabexc.org/posts/awesome-xautolock-battery
'''
import os
import psutil
from pulsectl import Pulse
import subprocess
import sys
import time
SHOULD_SLEEP = 1
# LAPTOP CHECK
'''
We will only shut off the screen, not have it sleep completely if it's a PC
'''
if os.path.isdir('/sys/class/power_supply/AC'):
IS_LAPTOP = 1
else:
IS_LAPTOP = 0
### AC check
'''
if open('/sys/class/power_supply/AC/online').read().strip() == '1':
print('ac')
SHOULD_SLEEP = 0
'''
### Sound check
# https://pypi.python.org/pypi/pulsectl
# equivalent to:
# cat /proc/asound/card*/pcm*/sub*/status | grep RUNNING
# pacmd list-sink-inputs | wc -l
pulse = Pulse('sleep-if-idle')
if len(pulse.sink_input_list()):
print('sound')
SHOULD_SLEEP = 0
### Disk I/O Check
# we're just using psutil
# https://pythonhosted.org/psutil/
# iostat -k
# https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats
# [5] sectors read
# [9] sectors written
# [11] ios currently in progress
# https://gist.github.com/mmalone/1081615/a37b09ce1d6ac6960742444c50f99728bffc9859
# https://gist.github.com/mmalone/1081615
# diskstats = open('/proc/diskstats').readline().split()
'''
http://unix.stackexchange.com/questions/225095/how-to-get-total-read-and-write-iops-in-linux
pidstat -d 2 5 | grep 1000 <-- gives user over 5s
overall we can't reliably see when disk is being used
doesn't help that file copying seems to be intermittent
'''
# Process Check
busy_procs = ['rsync', 'scp', 'cp', 'mv', 'pacaur', 'wget', 'curl', 'youtube-dl']
for proc in psutil.process_iter():
if proc.name() in busy_procs:
SHOULD_SLEEP = 0
break
# File I/O check
diskio1 = psutil.disk_io_counters()
time.sleep(1)
diskio2 = psutil.disk_io_counters()
read = diskio2.read_bytes - diskio1.read_bytes
write = diskio2.write_bytes - diskio1.write_bytes
# > 2MB read/write, works w/ thunar well
io = read + write
if read+write > 2000000:
SHOULD_SLEEP = 0
### DEBUG
# print(SHOULD_SLEEP)
# sys.exit()
### FINALLY, do we sleep?
if not SHOULD_SLEEP:
subprocess.call(['xautolock', '-restart'])
else:
if IS_LAPTOP:
subprocess.call(['systemctl', 'suspend'])
else:
subprocess.call(['xset', 'dpms', 'force', 'suspend'])