forked from anticapitalista/desktop-defaults-xfce-mx
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtumblerd-monitor
executable file
·62 lines (47 loc) · 1.43 KB
/
tumblerd-monitor
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
#!/bin/bash
#tumblerd-monitor, original script from arch-wiki
#how often script checks, I think
period=20
#file to monitor
tumblerpath="/usr/lib/*/tumbler-1/tumblerd" # The * here should find the right one, whether 32 and 64-bit
#thresholds to trip kill command
cpu_threshold=50
mem_threshold=20
max_strikes=2 # max number of above cpu/mem-threshold's in a row
#log file
log="/tmp/tumblerd-watcher.log"
#divert output to log file
if [[ -n "${log}" ]]; then
cat /dev/null > "${log}"
exec >"${log}" 2>&1
fi
#after sleep period, do monitor
main()
{
strikes=0
while true; do
sleep "${period}"
monitor
done
}
# check the tumbler pid and monitor do checks if the pid is present
monitor()
{
while read pid; do
cpu_usage=$(ps --no-headers -o pcpu -f "${pid}"|cut -f1 -d.)
mem_usage=$(ps --no-headers -o pmem -f "${pid}"|cut -f1 -d.)
if [[ $cpu_usage -gt $cpu_threshold ]] || [[ $mem_usage -gt $mem_threshold ]]; then
echo "$(date +"%F %T") PID: $pid CPU: $cpu_usage/$cpu_threshold %cpu MEM: $mem_usage/$mem_threshold STRIKES: ${strikes} NPROCS: $(pgrep -c -f ${tumblerpath})"
(( strikes++ ))
# if threshold are crossed kill tumblerd process
if [[ ${strikes} -ge ${max_strikes} ]]; then
kill "${pid}"
echo "$(date +"%F %T") PID: $pid KILLED; NPROCS: $(pgrep -c -f ${tumblerpath})"
strikes=0
fi
else
strikes=0
fi
done < <(pgrep -f ${tumblerpath})
}
main