I have a remote machine I need to keep an eye on. It's running Ubuntu Studio 22.04 (KDE Plasma). A few weeks ago it crashed and journalctl showed a "Bug" that occurred a couple minutes before the crash. So I wrote a simple script that follows journalctl and if the word "Bug" appears it sends a warning email. I set that script running about 10 days ago. Yesterday, I connected to the machine remotely and checked htop and found that the script was using over 90% of CPU. I killed it and CPU usage dropped back to normal. Here's the script:
#!/bin/bash
#####################
# THIS SCRIPT LAUNCHED AT STARTUP, CHECKS journalctl for string "Bug"
######################
while true; do
nohup journalctl --follow | grep -i -q "bug" && mutt -s "ALERT - AirchainPC may be in TROUBLE" -- [email protected] < bug_issued_by_journalctl.txt &>/dev/null &
done
Anything there that might explain the high CPU usage? BTW, I don't think I need that "nohup".
muttwhich otherwise isn't mentioned in the script at all, so are you just sending yourself the same file as an email infinitely many times?&in awhile true; ...loop will free the loop immediately resulting in rerunning the command sending hundreds/thousands to the background until your system resources are consumed ... Also detaching it withnohupwill do the same ... So, remove both and your script should run as you expect it without consuming all your system's resources.&at the end. OP's backgrounding everything in an infinite loop, so it doesn't matter what's going on withjournalctl,grep, ormutt, really. I'd like to know how much OP understands shell scripting before attempting an explanation)journalctlhas a--grepoption, this will avoid some overhead as well.