-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalert-bad-processes-user
76 lines (65 loc) · 2.25 KB
/
alert-bad-processes-user
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
#! /bin/bash
#----------------------------------------------------------------------
# Description: alert audibly and visually if there are CPU heavy
# processes running
# if argument1 is set then alert audibly
#
# 2026-01-11 23:11:09 v1.0.1 - made `top` version agnostic
# 2019-10-16 21:31:20 fine tune for LXDM *only*
# 2025-01-02 05:25:38 turned it into a user script to be run from ~/.config/autostart; fixed top's no config invocation (no option yet, filed a bug report)
# 2025-01-03 05:25:38 now uses a standard audio file
#
# Author: Artem S. Tashkinov
# Created at: Mon Jan 11 18:54:14 2016
# Computer: localhost.localdomain
# System: Linux 4.4.0-ic64 on x86_64
#
# Copyright (c) 2016-2025 Artem S. Tashkinov All rights reserved.
#----------------------------------------------------------------------
faudio=/usr/share/sounds/freedesktop/stereo/complete.oga
timeout=3 # visual cue for this number of seconds
threshold=60 # minimum CPU usage which is considered bad
volume=33 # audio notification volume
interval=5 # check for bad processes every X seconds
nl='
'
processes()
{
# doesn't work: shows the aggregated CPU usage which might be very low for long running processes
# ps auxS | awk '{if ($3 > 80) for (i=11; i<=NF; i++) print $i" "}'
# doesn't work: top version specific
# HOME=/dev/null top -bn1 | tail -n +8 | awk 'BEGIN{ORS=""}{if ($9 > '$threshold') {print $9"%\t"; for (i=12; i<=NF; i++) print $i" "; print "\n"}}'
# avoid using user's top settings
env HOME=/dev/null XDG_CONFIG_HOME="" top -bn1 | \
awk '
BEGIN {ORS=""}
{
if ($0~/PID.*USER.*PR/) {
for (i=1;i<=NF;i++) {
if ($i~/CPU/) f_cpu=i;
if ($i~/COMMAND/) f_cmd=i;
found=1;
}
}
if (found) {
if ($f_cpu > '$threshold') {
print $f_cpu"%\t";
for (i=f_cmd; i<=NF; i++) {
if ($i != "`-") # top 3.3 likes a tree structure
print $i" ";
}
print "\n";
}
}
}'
}
while :; do
bad_processes=`processes`
if [ -n "$bad_processes" ]; then
notify-send --expire-time="${timeout}000" --icon=/mnt/STORAGE/birdie/linux/new/Adwaita_32x32_dialog-warning.png "Warning!" "Detected > ${threshold}%:$nl$bad_processes" &> /dev/null &
if [ -f "$faudio" -a -n "$1" ]; then
paplay --volume=$((volume*65535/100)) "$faudio" &
fi
fi
sleep $interval
done