Skip to content

Instantly share code, notes, and snippets.

@0x0ade
Last active September 20, 2019 01:16
Show Gist options
  • Save 0x0ade/93d083546c88d4920d4b39ef96ff6509 to your computer and use it in GitHub Desktop.
Save 0x0ade/93d083546c88d4920d4b39ef96ff6509 to your computer and use it in GitHub Desktop.
#!/bin/bash
# throttle - utility throttling applications according to CPU temperature.
# Usage: throttle.sh <PID>
# Alternatively: throttle.sh <your command here>
# Version: 1
# Changelog:
# 1:
# * Configuration vars are only set if not already set (per-app config. via env.)
# * Added QUIET configuration
# * Added notify-send support
#
# 0:
# * Initial release.
#
#
# Issues:
# * SIGTSTP, SIGCONT let the task run in background if self-maintaining the job
PID=$1
ID=''
TAG='[throttle] '
#Configuration
THROTTLE_DEVICE=${THROTTLE_DEVICE:=k10temp-pci-00c3}
THROTTLE_HOT=${THROTTLE_HOT:=100}
THROTTLE_COLD=${THROTTLE_COLD:=95}
THROTTLE_POS=${THROTTLE_POS:=15}
THROTTLE_LEN=${THROTTLE_LEN:=3}
THROTTLE_SLEEP=${THROTTLE_SLEEP:=0.1}
THROTTLE_SHOWTIME=${THROTTLE_SHOWTIME:=5}
THROTTLE_QUIET=${THROTTLE_QUIET:=0}
THROTTLE_NOTIFY_QUIET=${THROTTLE_NOTIFY_QUIET:=0}
THROTTLE_JOB_QUIET=${THROTTLE_JOB_QUIET:=0}
_TEMPHOTSHOWTIME=0
_SHOWINGTEMP=0
KILLONEXIT=0
PAUSED=1
if ! kill -0 "${PID}" 2> /dev/null; then
if [ "$THROTTLE_JOB_QUIET" = "1" ]; then
( setsid "$@" &> /dev/null ) &
else
( setsid "$@" ) &
fi
PID=$!
ID="$@"
KILLONEXIT=1
PAUSED=0
fi
function log {
if [ "$THROTTLE_QUIET" != "1" ]; then
echo ${TAG}${@}
fi
if [ "$THROTTLE_NOTIFY_QUIET" != "1" ]; then
notify-send -u normal -a throttle -i applications-other "$1" "$2"
fi
}
function logw {
log '[WARNING] '${@}
}
function sig {
if ! kill -s $1 $PID 2> /dev/null; then logw 'kill can'\''t '${1}' PID '${PID}; fi
}
function sigg {
if ! kill -s $1 -$PID 2> /dev/null; then logw 'kill can'\''t '${1}' PID group '${PID}; sig $@; fi
}
if [ "$KILLONEXIT" = "1" ]; then
trap 'cont; sigg SIGTERM' SIGTERM
trap 'cont; sigg SIGINT' SIGINT
# trap 'sigg SIGTSTP; exit' SIGTSTP
# trap 'sigg SIGCONT; exit' SIGCONT
else
trap 'cont; exit' SIGTERM
trap 'cont; exit' SIGINT
fi
log 'Throttling process '${PID} 'based on temperature of '${THROTTLE_DEVICE}' (Pausing at '${THROTTLE_HOT}'°C, continuing at '${THROTTLE_COLD}'°C)'
function stop {
if [ "$PAUSED" = "1" ]; then return 0; fi
log 'HOT ('${THROTTLE_HOT}'°C) - Pausing '${PID} $ID
#STOP or TSTP
sigg SIGSTOP
PAUSED=1
}
function cont {
if [ "$PAUSED" = "0" ]; then return 0; fi
log 'COLD ('${THROTTLE_COLD}'°C) - Unpausing '${PID} $ID
sigg SIGCONT
PAUSED=0
}
while kill -0 $PID 2> /dev/null; do
TEMP=$(expr substr "$(sensors $THROTTLE_DEVICE | grep temp1\: | grep high)" $THROTTLE_POS $THROTTLE_LEN | tr -d +.)
# echo $TAG'[debug] '$TEMP
# TEMP=100
if expr $THROTTLE_HOT '<=' $TEMP > /dev/null; then stop
elif expr $TEMP '<=' $THROTTLE_COLD > /dev/null; then cont
fi
if [ "$PAUSED" = "1" -a "$THROTTLE_QUIET" != "1" ]; then
if [ "$_TEMPHOTSHOWTIME" = "0" ]; then
if [ "$_SHOWINGTEMP" = "1" ]; then tput cuu 1; tput el; fi #cursor up 1; clear to end of line
echo ${TAG}'Current temp.: '${TEMP}'°C; HOT: '${THROTTLE_HOT}'°C; COLD:'${THROTTLE_COLD}'°C'
_SHOWINGTEMP=1
fi
_TEMPHOTSHOWTIME=$((($_TEMPHOTSHOWTIME + 1) % $THROTTLE_SHOWTIME))
_SHOWINGTEMP=1
else
_TEMPHOTSHOWTIME=0
_SHOWINGTEMP=0
fi
sleep $THROTTLE_SLEEP
done
log 'Process '${PID}' completed.' $ID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment