To limit the CPU usage of processes, a shell script code is provided as follows:
Renice + 10 'ps aux | awk' {if ($3> 0.8 & id-u $1> 500) print $2 }''
Ps is used to obtain process information. In fact
The % CPU column in ps indicates the ratio of the actual CPU time occupied by the process to its survival time. This value reflects the CPU consumption of the process, however, the percentage of CPU time occupied by processes to the CPU of the entire system cannot be accurately reflected.
The % CPU column in the top output is the percentage of CPU time occupied by the process to the CPU of the entire system. It is used to limit the CPU usage of the process to a more reasonable value, the % MEM column also reflects the percentage of memory occupied by processes, which can be used to limit the memory usage of processes.
The shell script code is as follows:
#! /Bin/sh
PIDS = 'top-bn 1 | grep "^ * [1-9]" | awk '{if ($9> 50 | $10> 25 & id-u $ 2> 500) print $1 }''
For PID in $ PIDS
Do
Renice + 10 $ PID
Echo "renice + 10 $ PID"
Done
You can run this script in cron. For example, you only need to add the crontab entry as root when checking every minute:
# Crontab-e
* ***** Limit. sh
In the next minute, the system checks the nice values of processes that occupy more than 50% of the CPU or 25% of the memory. This lowers the priority of such processes and reduces the chances of being scheduled, at the same time, an email is sent to the root, prompting that the process has been adjusted.
However, it is best to use PAM to restrict memory usage. RedHat can be set in/etc/security/limits. conf.
Referenced in: http://linux.chinaitlab.com/administer/827444.html