Last active
December 25, 2015 00:39
-
-
Save Termiux/6889447 to your computer and use it in GitHub Desktop.
Limit php process using cpulimit program on linux boxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# /etc/init.d/limitcpu | |
# | |
# Some things that run always | |
# | |
INTERVAL=30 | |
LIMIT=75 | |
PIDS=$(top -b -n1 -c | grep php | awk '$9>75 {print $1}') | |
MYPID=$(ps aux | grep /etc/init.d/limitcpu | grep -v grep | awk '{print $2}') | |
checkAndLimit() | |
{ | |
# Search and limit violating PIDs | |
while sleep $INTERVAL | |
do | |
for i in $PIDS | |
do | |
#echo $i | |
cpulimit --pid=$i -l $LIMIT -z -b & # Limit new violating processes | |
logger "$i CPU limited to $LIMIT" | |
done | |
done | |
} | |
# Carry out specific functions when asked to by the system | |
case "$1" in | |
start) | |
logger "$0 Starting up with check interval of $INTERVAL..." | |
checkAndLimit & | |
;; | |
stop) | |
logger "$0 Stoping script..." | |
kill -9 $MYPID | |
;; | |
*) | |
echo "Usage: /etc/init.d/$0 {start|stop}" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment