Here is an improved script, you can run it manually:
#!/bin/bash
#v. 0.0.0
#cpu minimal limit to log
cpuLimit=50;
#log files
cpuLog="/var/log/cpu-log.log";
cpuLimitLog="/var/log/cpu-limit-log.log";
while true;
###cpuUsage=$(top -bn2 | grep '%Cpu' | tail -1 | grep -P '(....|...) id,'|awk '{print 100-$8}');
cpuUsage=$( cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}' );
#comment this next line out if you don't need to log all resource limits
do (echo; echo "CPU usage ${cpuUsage%.*}: $(date)" && ps -e -o pcpu,pmem,user,group,vsz,nice,args --sort=-pcpu | head -10 | cut -d" " -f1- ) >> $cpuLog;
#log if cou utilisation is above a limit
if [ ${cpuUsage%.*} -ge $cpuLimit ]
then
#uncomment next just to see if works
#echo ${cpuUsage%.*};
( echo; echo "CPU usage ${cpuUsage%.*}: $(date)" && ps -e -o pcpu,pmem,user,group,vsz,nice,args --sort=-pcpu | head -10 | cut -d" " -f1- ) >> $cpuLimitLog;
fi;
sleep 5;
done
You can change the cpu limit to log (in your case you want only 100, but I put 50 for testing purposes)
cpuLimit=50;
And now I sorted out to show the processes that really eat up cpu, here is an example of the output (you can change the log file location, I change them to /var/log/cpu-limit-log.log…)
CPU usage 54: Fri Sep 1 06:24:37 EEST 2023
%CPU %MEM USER GROUP VSZ NI COMMAND
21.0 1.9 root root 123788 0 /usr/share/webmin/authentic-theme/stats.cgi
5.3 11.4 root root 1002776 0 /usr/bin/suricata -D --af-packet -c /etc/suricata/suricata.yaml --pidfile /run/suricata.pid
4.6 0.2 root root 167756 0 /sbin/init
1.1 0.7 root root 1551532 0 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
0.0 0.0 root root 0 0 [kthreadd]
0.0 0.0 root root 0 -20 [rcu_gp]
0.0 0.0 root root 0 -20 [rcu_par_gp]
0.0 0.0 root root 0 -20 [slub_flushwq]
0.0 0.0 root root 0 -20 [netns]
CPU usage 60: Fri Sep 1 06:25:02 EEST 2023
%CPU %MEM USER GROUP VSZ NI COMMAND
72.0 0.0 root root 3644 0 gzip -f -9 /var/log/account//pacct.0
9.6 1.8 root root 128532 0 /usr/share/webmin/virtual-server/collectinfo.pl
5.3 11.4 root root 1002776 0 /usr/bin/suricata -D --af-packet -c /etc/suricata/suricata.yaml --pidfile /run/suricata.pid
4.6 0.2 root root 167756 0 /sbin/init
2.0 1.4 root root 110656 0 /usr/share/webmin/webmincron/webmincron.pl
1.1 0.7 root root 1551532 0 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
0.0 0.0 root root 0 0 [kthreadd]
0.0 0.0 root root 0 -20 [rcu_gp]
0.0 0.0 root root 0 -20 [rcu_par_gp]
CPU usage 50: Fri Sep 1 06:26:30 EEST 2023
%CPU %MEM USER GROUP VSZ NI COMMAND
11.0 0.0 root root 0 0 [/usr/share/webm] <defunct>
6.2 1.8 root root 120216 0 /usr/share/webmin/webmin/refresh_modules.cgi
5.3 11.4 root root 1002776 0 /usr/bin/suricata -D --af-packet -c /etc/suricata/suricata.yaml --pidfile /run/suricata.pid
4.6 0.2 root root 167756 0 /sbin/init
1.1 0.7 root root 1551532 0 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
0.0 0.0 root root 0 0 [kthreadd]
0.0 0.0 root root 0 -20 [rcu_gp]
0.0 0.0 root root 0 -20 [rcu_par_gp]
0.0 0.0 root root 0 -20 [slub_flushwq]
Hopefully is more useful than last solution I gave you, for you and for anyone else…
Anyway, sorry for before, was first time coding in bash but that is no excuse.