Limits CPU and memory usage in Linux

Source: Internet
Author: User

View CPU Footprint

Enter top on the command line

You can start top

Top Full Screen Dialog mode can be divided into 3 parts: System Information Bar, command input bar, process list bar.

Use top to view system load

The top command can dynamically monitor the system load, including CPU, memory usage, and resource-intensive process details.

Dynamically View system load (occupy one terminal)

Top

Exit after display

Top-n 1

Output in text format for use by other programs

Top-b

All right, let's see. Limit CPU and memory usage methods

Script content:

The code is as follows

#!/bin/sh
Upid= ' Top-bn 1 | grep "^ *[1-9]" | awk ' {if ($ > | | $ > && id-u $ >) print '
For PID in $UPID
Todo
Renice +10 $PID
echo "Renice +10 $PID"
Done

I modified the script above to limit the process to the entire server.

Modified script:

The code is as follows

#!/bin/sh
Upid= ' Top-bn 1 | grep "^ *[1-9]" | awk ' {if ($ > | | $ >) PRINT $} '
For PID in $UPID
Todo
Renice +10 $PID
echo "Renice +10 $PID"
Done

You can run this script in cron and have it checked every minute:

The code is as follows
chmod +x limit.sh
Vi/etc/crontab
*/1 * * * */bin/sh/root/soft_shell/limit.sh

Ps:

N: Number of updates, and will exit top after completion

B: Batch file mode, with "n" Parameters used together, can be used to output the top results to the file

So top-bn 1 is the result of getting the top output once.

grep "^*[1-9]": To get the PID number directly, excluding the table first information.

awk ' {if ($ > | | $ > && id-u $ >) print ':

Process PID with CPU usage greater than 20% or memory usage greater than 25%, and user uid greater than 500.

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.