Skip to content

Instantly share code, notes, and snippets.

@endolith
Last active June 5, 2023 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endolith/02bc3cf6d6deb879c5cbb530a83f15b7 to your computer and use it in GitHub Desktop.
Save endolith/02bc3cf6d6deb879c5cbb530a83f15b7 to your computer and use it in GitHub Desktop.
Turn BOINC on or off depending on how warm the room is

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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment