Skip to content

Commit 28d1310

Browse files
alert-bad-processes: alert in case something uses 100% of CPU
Put this script into crontab, e.g. */2 * * * * root test -f /root/bin/alert-bad-processes && /root/bin/alert-bad-processes 0 1 &> /dev/null A Linux script which could alert you (visually or/and audibly) when there is/are process(es) which use(s) 100% of CPU. It's written specifically for X.org. It's currently fine-tuned for LXDM but there's a universal version which is suitable if `w` works for you.
1 parent 722c81f commit 28d1310

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

alert-bad-processes

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#! /bin/bash
2+
3+
#----------------------------------------------------------------------
4+
# Description: alert audibly and visually if there are CPU heavy
5+
# processes running
6+
# if argument1 == 1 then alert audibly
7+
# if argument2 == 1 then alert visually
8+
#
9+
# Mon Jan 11 23:11:09 2016 v1.0.1 - made `top` version agnostic
10+
# Wed 16 Oct 2019 09:31:20 PM - fine tune for LXDM *only*
11+
#
12+
# Author: Artem S. Tashkinov
13+
# Created at: Mon Jan 11 18:54:14 2016
14+
# Computer: localhost.localdomain
15+
# System: Linux 4.4.0-ic64 on x86_64
16+
#
17+
# Copyright (c) 2016-2019 Artem S. Tashkinov All rights reserved.
18+
#----------------------------------------------------------------------
19+
20+
faudio=/opt/kde3/share/sounds/KDE_Beep_ClockChime.wav
21+
timeout=3 # visual cue for this number of seconds
22+
threshold=75 # minimum CPU usage which is considered bad
23+
24+
nl='
25+
'
26+
27+
processes()
28+
{
29+
# doesn't work: shows the aggregated CPU usage which might be very low for long running processes
30+
# ps auxS | awk '{if ($3 > 80) for (i=11; i<=NF; i++) print $i" "}'
31+
32+
# doesn't work: top version specific
33+
# 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"}}'
34+
35+
# avoid using user's top settings
36+
HOME=/dev/null top -bn1 | \
37+
awk '
38+
BEGIN {ORS=""}
39+
{
40+
if ($0~/PID.*USER.*PR/) {
41+
for (i=1;i<=NF;i++) {
42+
if ($i~/CPU/) f_cpu=i;
43+
if ($i~/COMMAND/) f_cmd=i;
44+
found=1;
45+
}
46+
}
47+
if (found) {
48+
if ($f_cpu > '$threshold') {
49+
print $f_cpu"%\t";
50+
for (i=f_cmd; i<=NF; i++) {
51+
if ($i != "`-") # top 3.3 likes a tree structure
52+
print $i" ";
53+
}
54+
print "\n";
55+
}
56+
}
57+
}'
58+
}
59+
60+
if [ "$1" = 1 ]; then
61+
if [ -n "`processes`" ]; then
62+
if [ -f "$faudio" ]; then
63+
aplay -q "$faudio" &
64+
elif eplay --version &>/dev/null; then
65+
play -q -n synth 0.5 sine &
66+
else
67+
echo -ne '\007' &
68+
fi
69+
fi
70+
fi
71+
72+
# LXDM version
73+
pid_lxdm=`pidof lxdm-session`
74+
Xuser=0 # this might not work - needs to be tested
75+
test -z "$pid_lxdm" || Xuser=`ps -u --ppid "$pid_lxdm" | tail -1 | awk '{print $1}'`
76+
77+
if [ "$2" = 1 -a -n "$Xuser" ]; then
78+
bad_processes=`processes`
79+
if [ -n "$bad_processes" ]; then
80+
su "$Xuser" -c "DISPLAY=:0 xmessage -center -timeout $timeout 'Detected > ${threshold}%:$nl$bad_processes' &> /dev/null" &
81+
fi
82+
fi
83+
84+
# Universal version in case w does work (it doesn't for LXDM)
85+
#if [ "$2" = 1 ]; then
86+
# w | awk '/xdm/{print $1" "$2}' | while read current_user current_x; do
87+
# bad_processes=`processes`
88+
# if [ -n "$bad_processes" ]; then
89+
# su $current_user -c "DISPLAY=$current_x xmessage -center -timeout $timeout 'Detected > ${threshold}%:$nl$bad_processes' &> /dev/null" &
90+
# fi
91+
# done
92+
#fi

0 commit comments

Comments
 (0)