The batch processing mode of skillfully using Linux-top

Source: Internet
Author: User

It technology learning: One is the use of gradual and systematic learning, one is the use of "opportunistic" fragmented learning. I this person is more Lai, also did not have those the spirit and the level which the Daniel is persistent, therefore can only share the latter with everybody

The top command-is a system management tool that comes with the unin&&linux system. Multiple metrics such as system performance (load, process, run time) can be displayed in real time. Popular web tutorials are taught in an interactive (real-time) way, and in fact top comes with a powerful batch mode that helps you do more with less. Usually in
Interactive mode, you only need to run top directly under the shell, as follows

The real-time mode benefit is the ability to observe system status in real time, but the drawbacks are obvious

    1. How to record and view a record of a value (historical and current) without regard to the use of third-party monitoring tools
      For example, I need to know how many tasks the system runs at a certain time, do I need to stare at the screen?
    2. How do I handle the System Metrics collection shell script? The result needs to be output first. Then you can use the logic calls you need to enter the input, processing is completed before the output
      These can all be handled in batch mode.

top-Batch mode starts at-shell with option B to control

    • -b:runs Top in batch mode
      With batch mode, the core is the ability to export content
      Let's take a look at the first example

Export the top output once

top -b -n 1 > top.txtcat top.txt

You will be surprised to find that the top content can be exported to file

Of course, you can take the snapshot continuously with the-n parameter

top -b -n 5 > top-5iterations.txtcat top-5iterations.txt | grep Tasks

Get 5 snapshots, then filter out tasks

Next, we'll look at the output of the process with Crond (scheduled task process)

$ pidof crond #获取pid$ top -p 536 -b -n3 > cron.txt #输出$ cat cron.txt

Finally, let's combine a shell command to get the process in succession, as follows

for i in {1..8}; do sleep 2 && top -b -p 536 -n1 | tail -1 ; done >> cron.txtcat cronta.txt

In the actual effect, in order to let everyone know the meaning of each column, I display the header field name for each column, as follows

We can also use batch mode to sort the current process memory usage

top -b -o +%CPU | head -n 22 #排列最占用CPU的进程

This article is just a start, specific use, please actively open their brains
The above test system CENTOS7.2 other Linux hairstyles, please Test yourself

Use the Linux-top batch mode skillfully

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.