Written by ChatGPT and me.
I also had to enable temperhum as a non-root user and set up a cron job to call this script every once in a while.
| #!/usr/bin/env python3 | |
| import subprocess | |
| import re | |
| import datetime | |
| import os | |
| # Set the temperature threshold (in Celsius) | |
| TEMP_THRESHOLD = 25 | |
| # Set the path to the BOINC data directory containing the gui_rpc_auth.cfg file | |
| boinc_data_dir = "/var/lib/boinc-client" | |
| # Get the current temperature from TemperHum | |
| temperhum_output = subprocess.check_output("temper-poll -c", shell=True).decode("utf-8") | |
| current_temperature = float(temperhum_output.strip()) | |
| # Set the absolute path to the log file | |
| log_file = "/home/endolith/temperature/boinc_temperature_control.log" | |
| # Change the working directory to the BOINC data directory | |
| os.chdir(boinc_data_dir) | |
| # Get project status | |
| project_status_output = subprocess.check_output("boinccmd --get_project_status", shell=True).decode("utf-8") | |
| # Extract project URLs and their "don't request more work" status | |
| projects = re.findall(r'master URL: (.+?)\n.*?don\'t request more work: (\w+)', project_status_output, re.DOTALL) | |
| # Check if the temperature is above the threshold | |
| if current_temperature > TEMP_THRESHOLD: | |
| # Disable new tasks for all projects only if they are not already suspended | |
| for project_url, no_more_work in projects: | |
| if no_more_work == "no": | |
| subprocess.run(f"boinccmd --project {project_url} nomorework", shell=True) | |
| with open(log_file, "a") as log: | |
| log.write(f"{datetime.datetime.now()} - Temperature: {current_temperature}°C - BOINC tasks suspended for project {project_url}\n") | |
| else: | |
| # Enable new tasks for all projects only if they are not already running | |
| for project_url, no_more_work in projects: | |
| if no_more_work == "yes": | |
| subprocess.run(f"boinccmd --project {project_url} allowmorework", shell=True) | |
| with open(log_file, "a") as log: | |
| log.write(f"{datetime.datetime.now()} - Temperature: {current_temperature}°C - BOINC tasks resumed for project {project_url}\n") |