我创建了一个脚本,该脚本可以在CPU和/或内存使用率达到80%时终止进程。发生这种情况时,它将创建一个被终止进程的列表。我该怎么做才能改善它?
while [ 1 ];
do
echo
echo checking for run-away process ...
CPU_USAGE=$(uptime | cut -d"," -f4 | cut -d":" -f2 | cut -d" " -f2 | sed -e "s/\.//g")
CPU_USAGE_THRESHOLD=800
PROCESS=$(ps aux r)
TOPPROCESS=$(ps -eo pid -eo pcpu -eo command | sort -k 2 -r | grep -v PID | head -n 1)
if [ $CPU_USAGE -gt $CPU_USAGE_THRESHOLD] ; then
kill -9 $(ps -eo pid | sort -k 1 -r | grep -v PID | head -n 1) #original
kill -9 $(ps -eo pcpu | sort -k 1 -r | grep -v %CPU | head -n 1)
kill -9 $TOPPROCESS
echo system overloading!
echo Top-most process killed $TOPPROCESS
echo CPU USAGE is at $CPU_LOAD
else
fi
exit 0
sleep 1;
done
sleep 1
在循环中
while [ 1 ]
让我想知道仅此脚本将消耗多少CPU。另外,kill -9
在脚本中有3个调用持续运行吗?这让我发冷...