I'm trying to create a daemon to monitor my system's CPU temps and adjust the clock-rate if it gets too high, but I've never written a daemon before and I'm not sure I've done any of it right.
I created two files in a folder inside of /usr/local/lib as according to the file-heirarchy, called throttle_daemon inside of which is throttle_daemon and throttle_daemon.service, then I linked throttle_daemon.service to /etc/systemd/system/throttle_daemon.service.
This is the throttle_daemon
# !/bin/bash
CPU_TEMP=$(sensors -f | grep -Po "Tdie:\s*\+\d+" | grep -Po "\d+")
function change_frequency {
for cpu in "/sys/devices/system/cpu/cpu*/"; do
cpu=${cpu%*/} # Remove the trailing "/"
echo "$1" | sudo tee "$cpu/cpufreq/scaling_max_freq"
done
}
notify-send "CPU Throttle Daemon" "CPU Temp is $CPU_TEMP"
if [[ $CPU_TEMP -ge 122 ]]; then
notify-send "CPU Throttle Daemon" "Throttling CPU"
touch /var/tmp/throttle.flag
change_frequency 2200000
elif [[ $CPU_TEMP -le 113 ]]; then
if [[ -f /var/tmp/throttle.flag ]]; then
notify-send "CPU Throttle Daemon" "Un-Throttling CPU"
change_frequency 3600000
rm /var/tmp/throttle.flag
fi
fi
And my throttle_daemon.service
[Unit]
Description="CPU Throttle Service"
[Service]
NotifyAccess=all
Restart=always
RestartSec=1s
ExecStart=/usr/local/lib/throttle_daemon/throttle_daemon
[Install]
WantedBy=multi-user.target
When I run the script from the command line it works as expected, but not when I set up the service. When I call sudo systemctl start throttle_daemon.service nothing errors out, but it also doesn't do anything.
I expected notify-send to ping me every second with the current temperature that my cpu is at, why isn't it?