Shell script limits the CPU and memory usage of user processes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.