Skip to main content
Notice removed Draw attention by CommunityBot
Bounty Ended with no winning answer by CommunityBot
Notice added Draw attention by iggy12345
Bounty Started worth 50 reputation by iggy12345
Updated .service file
Source Link
iggy12345
  • 123
  • 1
  • 12

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?

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?

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?

Fixed buggy code and added another usage example
Source Link
iggy12345
  • 123
  • 1
  • 12

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+")

function# change_frequencysu {- 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 "$1""3200000" | sudo tee "$cpu/cpufreq/scaling_max_freq"
    done
}

notifyelif [ $CPU_TEMP -sendle "CPU113 Throttle]; Daemon"then
 "CPU Temp is $CPU_TEMP"

if [[ $CPU_TEMP[ -gef 122/var/tmp/throttle.flag ]];]; then
    notify    su - aaron -c "notify-send "CPU'CPU Throttle Daemon"Daemon' "Throttling'Un-Throttling CPU"CPU'"
    touch    for cpu in /varsys/tmpdevices/throttle.flagsystem/cpu/cpu*/; do
    change_frequency 2200000
elif [[ $CPU_TEMP -le 113 ]]; then
 cpu=${cpu%*/}  # ifRemove [[the -ftrailing /var/tmp"/throttle.flag"
 ]]; then
        notify-send "CPU Throttleecho Daemon""3600000" "Un-Throttling| CPU"
sudo tee "$cpu/cpufreq/scaling_max_freq"
      change_frequency 3600000 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?

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?

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?

Source Link
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

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?