LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2015, 08:46 AM   #1
xl
Member
 
Registered: Feb 2013
Location: Germany
Distribution: Stretch (stable)
Posts: 52

Rep: Reputation: 8
Bash script: Automatic BOINC temperature regulation


Hi all,

I wrote a simple bash script to change the CPU usage according to its temperature.
I wrote this for my AMD Athlon 64 X2 (R) [2006] on Debian Wheezy (should also work on Ubuntu ...?). You'll need the packages lm-sensors and sed. Note that you have to start this as root or with sudo rights (to change the BOINC owned preferences file).

The only thing I haven't got rid of is that it will output twice at start if you hit a time that corresponds with the OUTPUTPAUSE.
Suggestions and changes are welcome. I will update this post.

Use your favorite internet search enginge for your CPU's suggested temperature and set TARGTEMP.

I've set the interval of usage changes to 25 seconds and value of usage change to 4 %. This gives me quite stable output to the terminal. I'm not sure of decreasing the interval or increasing the value because this might influence the crunch results negatively.

Code:
#!/bin/bash
#Script to adapt BOINC's global CPU usage to CPU temperature
#Use your favorite internet search enginge
#for your CPU's suggested temperature
#Written for AMD Athlon 64 X2 (R) [2006]
#
#Released into the Public Domain or CC0, your choice.
#Thanks to grymoire.com, tldp.org, stackoverflow.com
#and all of the Free, Libre, Open Source Community.
#Version 0.2
#
#Usage:
#You can give the CPU usage as an argument and the script
#will set it and exit
#Run it without argument and it will adjust according to temperature

#Set target temperature of your CPU in Degree Celsius here:
TARGTEMP=50

#Value changes of CPU usage in percent
USAGELEAPS=4
#Can be adjusted in line 60 and 61 (e. g. -1 or +5).

#Set interval between usage changes (in seconds)
CHANGEPAUSE=25

#Set interval between screen outputs (in minutes)
OUTPUTPAUSE=5

#Simple check for dependencies
DEPENDENCIES="sensors sed"
for i in $DEPENDENCIES; do
if ! command -v "$i" >/dev/null
then echo "Please check if you have the package necessary for \033[1m$i\033[0m installed." && exit 1; fi; done

#Display (a kind of) progress bar graph of advised CPU usage
output () {
	echo -n "$CURRTEMP C "
	echo -n $(date +"%H:%M")
	for i in $(seq 1 "$ADVCPU"); do echo -n "="; done
	echo -n "$ADVCPU"% && echo
}

#Change to BOINC_DIR and create temporary folder for backup
BOINCDAT=$(grep BOINC_DIR /etc/default/boinc-client | grep -v "^#" | cut -d"\"" -f2)
cd $BOINCDAT
mkdir -p tmp

#Set counter before "while loop" (v)
COUNTER=0

#Begin loop if BOINC is running
while [ -f "lockfile" ]; do
#Grab current CPU usage settings
CURRCPU=$(grep "<cpu_usage_limit>" global_prefs_override.xml | cut -d">" -f2 | cut -d"." -f1)
#Grab current temperature
CURRTEMP=$(sensors | grep "Core1 Temp" | head -1 | cut -d"+" -f2 | cut -d"." -f1)
#In-/Decrease usage by ^USAGELEAPS
if [[ "$CURRTEMP" -gt "$TARGTEMP"  &&  "$CURRCPU" -gt "$USAGELEAPS" ]]
	then ADVCPU=$(( $CURRCPU-$USAGELEAPS ))
	else ADVCPU=$(( $CURRCPU+$USAGELEAPS ))
fi

#If argument given set it as CPU usage
[ -n "${1}" ] && ADVCPU=$1

#Puts out advised CPU usage once at start (^ COUNTER=0)
if [ "$COUNTER" = 0 ]; then
	output
	COUNTER=1
fi

#Calculates pause for output on screen and to log by remainder
MINUTE=$(date +"%-M")
REMAINDER=$(( $MINUTE % $OUTPUTPAUSE ))
if [ "$REMAINDER" = 0 ] && [ "$MINUTE" != "$OLDMINUTE" ]; then
	output
#Save time to compare as to not output more than once in the same minute	
OLDMINUTE="$MINUTE"
fi

#Stream EDit line with cpu_usage_limit by substituting CURRCPU with ADVCPU
sed '/cpu_usage_limit/ s/'$CURRCPU'/'$ADVCPU'/' <global_prefs_override.xml >tmp/global_prefs_override.xml
#Check temporary backup and copy to original (errors go to output AND into boinctemp.log)
if grep "<cpu_usage_limit>"$ADVCPU"" tmp/global_prefs_override.xml 2>&1>/dev/null | tee -a boinctemp.log
	then cp tmp/global_prefs_override.xml global_prefs_override.xml
	else echo "Can't edit preferences file. Check your disk." && exit 1
fi
boinccmd --read_global_prefs_override
#if usage is given as argument, exit
[ -n "${1}" ] && exit
sleep "$CHANGEPAUSE"s
done
Apologies to other flavour users but this should be easily adaptable.
 
Old 07-04-2018, 10:04 AM   #2
xl
Member
 
Registered: Feb 2013
Location: Germany
Distribution: Stretch (stable)
Posts: 52

Original Poster
Rep: Reputation: 8
Can I change the original post?

Here's v0.5 of the script:
Code:
#!/bin/bash
#Script to adapt BOINC's global CPU usage to CPU temperature
#Use your favorite internet search enginge
#for your CPU's suggested temperature
#Written for AMD Athlon 64 X2 (R) [2006]
#
#Released into the Public Domain or CC0, your choice.
#Thanks to grymoire.com, tldp.org, stackoverflow.com
#and all of the Free, Libre, Open Source Community.
#Version 0.5
#
#Usage:
#You can give the CPU usage as an argument 
# and the script will set it and exit
#Run it without argument and it will adjust according to temperature

#Set target temperature of your CPU in Degree Celsius here:
TARGTEMP=60

#Value changes of CPU usage in percent
USAGELEAPS=4
#Can be adjusted in line 60 and 61.

#Set interval between usage changes (in seconds)
CHANGEPAUSE=60

#Set interval between screen outputs (in minutes)
OUTPUTPAUSE=1

#Simple check for dependencies
DEPENDENCIES="sensors sed"
for i in $DEPENDENCIES; do
if ! command -v "$i" >/dev/null
then echo "Please check if you have the package necessary for \033[1m$i\033[0m installed." && exit 1; fi; done

#Function for displaying (a kind of) progress bar graph of advised CPU usage
output () {
	echo -n "$CURRTEMP C "
	echo -n $(date +"%H:%M")
	echo -n " $ADVCPU"% && echo
}

#Change to BOINC_DIR and create temporary folder for backup
BOINCDAT=$(grep BOINC_DIR /etc/default/boinc-client | grep -v "^#" | cut -d"\"" -f2)
cd $BOINCDAT
mkdir -p tmp

#Set counter before "while loop" (v)
COUNTER=0

#Begin loop if BOINC is running
while [ -f "lockfile" ]; do
#Grab current CPU usage settings
	CURRCPU=$(grep "<cpu_usage_limit>" global_prefs_override.xml | cut -d">" -f2 | cut -d"." -f1)
#Grab current temperature
	CURRTEMP=$(sensors | grep "Core1 Temp" | head -1 | cut -d"+" -f2 | cut -d"." -f1)
#Make sure that advised CPU usage will not fall under 1
#In-/Decrease usage by ^USAGELEAPS
	if [[ "$TARGTEMP" -ge "$CURRTEMP" ]]
	then if [[ "$ADVCPU" -lt 100-"$USAGELEAPS" ]]
		then ADVCPU=$(( $CURRCPU+$USAGELEAPS ))
		else ADVCPU=99
	fi
	fi
	if [[ "$TARGTEMP" -lt "$CURRTEMP" ]]
	then if [[ "$ADVCPU" -gt 1+"$USAGELEAPS" ]]
		then ADVCPU=$(( $CURRCPU-$USAGELEAPS ))
		else ADVCPU=1
	fi
	fi	

#set CPU usage to 50 at start
[ "$COUNTER" -eq 0 ] && ADVCPU=50
#If argument given set it as CPU usage
[ -n "${1}" ] && ADVCPU=$1

#Sometimes puts advised CPU usage above 100%
#meaning that it will take longer to come back down 
#to normal percentage. If the script sets it to 0 
#it will also go to 100. So here's a warning:
	CHECKCPU=$(grep "<cpu_usage_limit>" global_prefs_override.xml | cut -d">" -f2 | cut -d"." -f1)
	if [[ "$CHECKCPU" -gt 99-"$USAGELEAPS" || "$CHECKCPU" -lt 2+"$USAGELEAPS" ]]
		then echo "Check BOINC's CPU usage limit!"
	fi

#Puts out advised CPU usage once at start (^ COUNTER=0)
if [ "$COUNTER" = 0 ]; then
	output
	COUNTER=1
fi

#Calculates pause for output on screen and to log by remainder
MINUTE=$(date +"%-M")
REMAINDER=$(( $MINUTE % $OUTPUTPAUSE ))
if [ "$REMAINDER" = 0 ] && [ "$MINUTE" != "$OLDMINUTE" ]; then
	output
#Save time to compare as to not output more than once in the same minute	
OLDMINUTE="$MINUTE"
fi

#Stream EDit line with CPU usage by substituting CURRCPU with ADVCPU
sed '/cpu_usage_limit/ s/'$CURRCPU'/'$ADVCPU'/' <global_prefs_override.xml >tmp/global_prefs_override.xml
#Check temporary backup and copy to original (errors go to output AND into boinctemp.log)
if grep "<cpu_usage_limit>"$ADVCPU"" tmp/global_prefs_override.xml 2>&1>/dev/null | tee -a boinctemp.log
	then cp tmp/global_prefs_override.xml global_prefs_override.xml
	else echo "Can't edit preferences file. Check your disk." && exit 1
fi
boinccmd --read_global_prefs_override
#if usage is given as argument, exit
[ -n "${1}" ] && exit
sleep "$CHANGEPAUSE"s
done

Last edited by xl; 07-04-2018 at 10:06 AM.
 
Old 07-12-2018, 10:16 AM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,856
Blog Entries: 36

Rep: Reputation: 456Reputation: 456Reputation: 456Reputation: 456Reputation: 456
Upload it to GitHub where changes can be tracked by interested people. After a time period you can no longer edit your own posts.
 
  


Reply

Tags
bash, boinc, linux, script, temperature


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script automatic file rename if exists AutoC Programming 4 06-23-2015 03:31 PM
Temperature inside computer box and CPU temperature Lola Kews Linux - Hardware 14 11-29-2013 06:22 PM
Bash script for automatic viewport changing and other WM stuff (works with compiz) Juako Programming 8 01-05-2011 06:35 AM
Console/ bash: Post your script to retrieve and echo the weather/temperature ? frenchn00b Linux - General 1 02-16-2008 09:45 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration