Skip to content

Instantly share code, notes, and snippets.

@lifeofcoding
Forked from hxer/cpulimit
Created June 2, 2020 23:56
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 lifeofcoding/bb3eeae0403a786f5f761ecc4b40199e to your computer and use it in GitHub Desktop.
Save lifeofcoding/bb3eeae0403a786f5f761ecc4b40199e to your computer and use it in GitHub Desktop.
cpulimit limit cpu usage for any process, ref:https://www.maketecheasier.com/limit-cpu-usage-of-any-process-in-linux/
#!/bin/sh
#
# Script to start CPU limit daemon
#
set -e
case "$1" in
start)
if [ $(ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh" {print $1}' | wc -l) -eq 0 ]; then
nohup /usr/bin/cpulimit_daemon.sh >/dev/null 2>&1 &
ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh" {print}' | wc -l | gawk '{ if ($1 == 1) print " * cpulimit daemon started successfully"; else print " * cpulimit daemon can not be started" }'
else
echo " * cpulimit daemon can't be started, because it is already running"
fi
;;
stop)
CPULIMIT_DAEMON=$(ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh" {print $1}' | wc -l)
CPULIMIT_INSTANCE=$(ps -eo pid,args | gawk '$2=="cpulimit" {print $1}' | wc -l)
CPULIMIT_ALL=$((CPULIMIT_DAEMON + CPULIMIT_INSTANCE))
if [ $CPULIMIT_ALL -gt 0 ]; then
if [ $CPULIMIT_DAEMON -gt 0 ]; then
ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh" {print $1}' | xargs kill -9 # kill cpulimit daemon
fi
if [ $CPULIMIT_INSTANCE -gt 0 ]; then
ps -eo pid,args | gawk '$2=="cpulimit" {print $1}' | xargs kill -9 # release cpulimited process to normal priority
fi
ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh" {print}' | wc -l | gawk '{ if ($1 == 1) print " * cpulimit daemon can not be stopped"; else print " * cpulimit daemon stopped successfully" }'
else
echo " * cpulimit daemon can't be stopped, because it is not running"
fi
;;
restart)
$0 stop
sleep 3
$0 start
;;
status)
ps -eo pid,args | gawk '$3=="/usr/bin/cpulimit_daemon.sh" {print}' | wc -l | gawk '{ if ($1 > 0) print " * cpulimit daemon is running"; else print " * cpulimit daemon is not running" }'
;;
esac
exit 0
#!/bin/bash
# ==============================================================
# CPU limit daemon - set PID's max. percentage CPU consumptions
# ==============================================================
# Variables
CPU_LIMIT=40 # Maximum percentage CPU consumption by each PID
DAEMON_INTERVAL=3 # Daemon check interval in seconds
# You can use the “|” delimiter to include multiple processes. For example, “mysql|firefox|gedit“.
BLACK_PROCESSES_LIST= # Limit only processes defined in this variable. If variable is empty (default) all violating processes are limited.
WHITE_PROCESSES_LIST= # Limit all processes except processes defined in this variable. If variable is empty (default) all violating processes are limited.
# Check if one of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST is defined.
if [[ -n "$BLACK_PROCESSES_LIST" && -n "$WHITE_PROCESSES_LIST" ]] ; then # If both variables are defined then error is produced.
echo "At least one or both of the variables BLACK_PROCESSES_LIST or WHITE_PROCESSES_LIST must be empty."
exit 1
elif [[ -n "$BLACK_PROCESSES_LIST" ]] ; then # If this variable is non-empty then set NEW_PIDS_COMMAND variable to bellow command
NEW_PIDS_COMMAND="top -b -n1 -c | grep -E '$BLACK_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
elif [[ -n "$WHITE_PROCESSES_LIST" ]] ; then # If this variable is non-empty then set NEW_PIDS_COMMAND variable to bellow command
NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6' | grep -E -v '$WHITE_PROCESSES_LIST' | gawk '\$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
else
NEW_PIDS_COMMAND="top -b -n1 -c | gawk 'NR>6 && \$9>CPU_LIMIT {print \$1}' CPU_LIMIT=$CPU_LIMIT"
fi
# Search and limit violating PIDs
while sleep $DAEMON_INTERVAL
do
NEW_PIDS=$(eval "$NEW_PIDS_COMMAND") # Violating PIDs
LIMITED_PIDS=$(ps -eo args | gawk '$1=="cpulimit" {print $3}') # Already limited PIDs
QUEUE_PIDS=$(comm -23 <(echo "$NEW_PIDS" | sort -u) <(echo "$LIMITED_PIDS" | sort -u) | grep -v '^$') # PIDs in queue
for i in $QUEUE_PIDS
do
cpulimit -p $i -l $CPU_LIMIT -z & # Limit new violating processes
done
done
#!/bin/bash
# Cpulimit - How to Limit The CPU Usage of Any Process in Linux
# Source: https://www.maketecheasier.com/limit-cpu-usage-of-any-process-in-linux/
if [[ -z $1 ]] ; then
echo "Usage: ./cpulimiter.sh install | uninstall"
exit 0
fi
if [[ $1 == "install" ]] ; then
sudo apt-get install cpulimit gawk
cd
mkdir cpulimit
cd cpulimit
wget https://raw.githubusercontent.com/sicambria/sh/master/system/cpulimit/cpulimit
wget https://raw.githubusercontent.com/sicambria/sh/master/system/cpulimit/cpulimit_daemon.sh
# Limit any process to use maximum 80% of a single CPU core
# Use 1 core fully = 100%
# Use 2 core fully = 200%
# etc.
sudo perl -pi -e 's?CPU_LIMIT=20?CPU_LIMIT=80?g' cpulimit_daemon.sh
sudo cp ~/cpulimit/cpulimit_daemon.sh /usr/bin
sudo chmod 700 /usr/bin/cpulimit_daemon.sh
sudo cp ~/cpulimit/cpulimit /etc/init.d/
sudo chown root:root /etc/init.d/cpulimit
sudo chmod +x /etc/init.d/cpulimit
sudo update-rc.d cpulimit defaults
sudo service cpulimit start
exit 0
fi
if [[ $1 == "uninstall" ]] ; then
sudo service cpulimit stop # Stop cpulimit daemon and all cpulimited processes
sudo update-rc.d -f cpulimit remove # Remove symbolic links
sudo rm /etc/init.d/cpulimit # Delete cpulimit boot-up script
sudo rm /usr/bin/cpulimit_daemon.sh # Delete cpulimit daemon script
sudo apt-get remove cpulimit
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment