Find the most CPU-consuming process in UNIX

Source: Internet
Author: User

During the running process of a multi-user UNIX computer system, the computing speed may suddenly slow down, and sometimes it may take a long time to even input characters from the keyboard to respond. Experienced people will know that the computer must be running a CPU-consuming process. Such a process is sometimes a person is executing a CPU-consuming program, sometimes it may be an unexpected situation in the system, and the system itself is processing. In either case, the system administrator should promptly find such a process and handle it accordingly.

Although UNIX provides a series of accounting programs such as "acct", these programs can only display running time, CPU usage time, and other information after the process ends, you cannot calculate which process occupies the most CPU time per unit time in real time.

For this reason, I wrote a program in shell language and implemented the above functions using some utilities provided by UNIX.

This program contains UNIX utilities such as ps, cut, and diff. The following describes the functions of these utilities.

Ps: displays information about processes in the current system. The-e parameter is used to display information about all processes in the system. Use the-f parameter to display the complete information of each process;

Cut: Used to crop files by column. The "-c-15, 33-" parameter indicates that all the characters before and after each line of the input file are put into the output file;

Echo: displays prompt information to the screen;

Sleep: allows the shell program to wait for several seconds and then execute the following statement;

Diff: used to compare two files. The differences are displayed;

Sort: sorts all rows in the file, and the sorting result is displayed;

Grep: used to find the rows that meet certain conditions in the file. The parameter "^" indicates finding the rows whose first column is space;

|: The pipeline symbol. The output of the preceding command can be used as the subsequent command input. In this way, the process of generating intermediate files can be omitted to improve the execution efficiency;

>: Redirects the output and outputs the content that should have been displayed on the screen to the file.

The program content is as follows:

Ps-ef | cut-c-15,33-> tt1

Echo Please wait a while...

Sleep 20

Ps-ef | cut-c-15,33-> tt2

Echo Attention!

Echo

Diff tt1 tt2 | cut-c 2-> tt3

Sort tt3 | grep ^ | cut-c-83 | grep-v 0: 00

Echo

Echo That is OK!

Rm tt1 tt2 tt3

The program first obtains information about all processes in the system and puts useful fields in the temporary file tt1. Then let the program wait for 20 seconds (the time can be adjusted according to the specific situation ).

Then, obtain information about all processes and put all fields in the temporary file tt2.

Compare the two temporary files to find out the processes with different information before and after 20 seconds (there are processes that have consumed CPU time changed ).

Remove the ">" and "<" generated during the comparison, and put the results in the temporary file tt3.

Sort the content in tt3, and sort the information of the same process that consumes CPU time changes together. Grep "^" is used to remove information about some intermediate command processes generated when the shell program is executed.

Use cut to delete the process information that is too long in each line to make the output clearer. Grep-v 0: 00 is used to remove the process that appears only once before and after 20 seconds. At this point, processes that consume different CPU time before and after 20 seconds are displayed on the screen, from which you can easily find the process you are looking. Finally, in order not to leave useless junk files in the system, delete all three temporary files.

  1. How to configure the Netware server in Linux
  2. Development of Linux systems on embedded devices
  3. In-depth introduction to how the Linux kernel works (1)

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.