Skip to main content
2 of 3
Fixed buggy code and added another usage example
iggy12345
  • 123
  • 1
  • 12

Debugging systemd service

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

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
export DISPLAY=:1

CPU_TEMP=$(sensors -f | grep -Po "Tdie:\s*\+\d+" | grep -Po "\d+")

# su - aaron -c "/usr/bin/notify-send 'CPU Throttle Daemon' 'CPU Temp is $CPU_TEMP'"

if [ $CPU_TEMP -ge 140 ]; then
    su - aaron -c "notify-send 'CPU Throttle Daemon' 'Throttling CPU'"
    touch /var/tmp/throttle.flag
    for cpu in /sys/devices/system/cpu/cpu*/; do
        cpu=${cpu%*/}  # Remove the trailing "/"
        echo "3200000" | sudo tee "$cpu/cpufreq/scaling_max_freq"
    done
elif [ $CPU_TEMP -le 113 ]; then
    if [ -f /var/tmp/throttle.flag ]; then
        su - aaron -c "notify-send 'CPU Throttle Daemon' 'Un-Throttling CPU'"
        for cpu in /sys/devices/system/cpu/cpu*/; do
            cpu=${cpu%*/}  # Remove the trailing "/"
            echo "3600000" | sudo tee "$cpu/cpufreq/scaling_max_freq"
        done
        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 using watch -n 1 sudo ./throttle_daemon 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?

iggy12345
  • 123
  • 1
  • 12