3

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]
Type=simple
BusName=unix:path=/run/usr/1000/bus
NotifyAccess=all
Restart=always
RestartSec=1s
Environment=DBUS_SESSION_BUS_ADDRESS=unix:abstract=/run/user/1000/bus
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?

3

I found the problem I was having was that I was missing the /bin/bash on my ExecStart= line

so I needed to change:

ExecStart=/usr/local/lib/throttle_daemon/throttle_daemon

to

ExecStart=/bin/bash /usr/local/lib/throttle_daemon/throttle_daemon

I was also missing the timeout configuration, I needed to add:

StartLimitBurst=0

to my [Service] section, after that, my program runs as expected.

I also changed WantedBy to graphical.target instead of multi.user.target because I'm running the desktop and I feel like the notifications would crash if I ran it on the terminal without the x server, but I can't verify that.

0

Unless I am mistaken, notify-send uses dbus to send the notification. First, your service runs as root by default as it is a system unit (based on the path you put your .service file). Second, if it must run as root, you need to make sure notify-send will be able to access your regular user's session dbus socket. Which typically in modern distros is /run/user/1000/bus (provided your user's id is 1000, see with id --user as this user).

You could add this to your unit file: Environment=DBUS_SESSION_BUS_ADDRESS=unix:abstract=/run/user/1000/bus

Not entirely sure if this will allow notify-send to properly send it's message. There may be policies (polkit or dbus policies) which prevent other users from talking to the software interfaces exposed by the session dbus daemon on the socket.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.