27 november 2017

To decrease the CPU demands for an application

Save the bash script below in a file called cpulimit.sh and give it executions rights with chmod.


-----CODE START----
#! /bin/bash
# Description:
# Tested on Macos and Mac OS X.
# If you want to decrease the CPU demands for an application
# you can use this very simple and ugly "hack". 
# I use it when I e.g convert videos on my MacBook Pro, compiling things, ...
# to prevent it from getting to warm. Even if the CPU is IDLE this simple hack
# will prevent the application to use the CPU during Sleep Time
# (in contrast to renice/nice commands which will take all IDLE time). 
# Hint: To get the PID (Process ID), first run command top -u in one Terminal window.
# Usage: ./cpulimitrob.sh
#
#
echo "Which process ID (PID)? "
read pid
echo "Sleep time in seconds? "
read sleeptime
echo "Run time in seconds (e.g 0.5 or 1 …)? "
read runtime
i=1
dot=.
while true
do
if [ $i -eq 1 ]
then
kill -SIGSTOP $pid
sleep $sleeptime
i=0
else
kill -SIGCONT $pid
sleep $runtime
i=1
fi
echo -n $dot
done
---CODE END----