Here come the next short bash scripts which show the simplicity and power of Bash / Linux. In order to manage heavy Linux processes during high CPU load we will do 2 things:
1. Try to re-nice the heavy processes each 2 mins. Sometimes this might help and allow an important process to complete while saving the system from overloading. For this purpose we set a cron to run each 2 mins:
*/2 * * * * test `cat /proc/loadavg |cut -d '.' -f1` -gt 5 && renice 19 `ps aux | sort -rn +2 2>/dev/null | head -1 | awk '{print $2}'`
In plain words this script means: Check if the average unix CPU load is higher than 5. If it is, renice to value 19 (the lowest priority a process can have) the most CPU consuming process.
2. Very often only renicing a process might not work and the system will continue to overload. For this purpose we will set the next cron to run each 10 mins:
*/10 * * * * test `cat /proc/loadavg |cut -d '.' -f1` -gt 15 && kill -9 `ps aux | sort -rn +2 2>/dev/null | head -1 | awk '{print $2}'`
This command will kill immedatialy the heaviest process when the system load is higher than 15. This could be a life saver.


