Author Information: Shenyang Bank Card Network Service Center Shi Li
Multi-user Linux systems are running during operation, sometimes the operation speed will suddenly slow, and even if you enter characters from the keyboard, you will have a react. In general, this is the reason why the computer is running a very costly CPU. Such a process is sometimes a man who is executing a very CPU, such as a program enters a dead cycle, sometimes it may be something out in the system, and the system itself is processed. No matter which case, system administrators should find out in time and make corresponding processing.
Although Linux provides some processing programs, these programs can only display information such as runtime and occupied CPU time after the process, which cannot be used in real time which process takes more CPU time. The author wrote a program with the shell language, using some utilities provided by Linux, realized real-time monitoring of the process.
Utility
Linux utilities such as PS, CUT, DIFF in Shell, and their features are simple to introduce as follows:
● PS: Used to display information about the process in the current system, plus the simple information of all processes in the system in the system, using the -f parameter, display the complete information of the process;
● CUT: It is used to cut the file in units, and the parameters "-c -15, 33-" indicate that the input file is 15 characters and the 33rd characters, and all characters of the end are placed in the output file;
● Echo: Used to display prompt information on the screen;
● SLEEP: Let the shell program waits for several seconds, then execute the following statement;
● DIFF: It is used to compare two files and display it differently;
● SORT: Used to sort the rows in the file, you can display the sort result;
● GREP: Used to find a line that meets a certain condition in the file, the parameter "^" means to find the first list of the first list as space.
In addition, "|" is "|" for pipe symbols, and the output of the previous command can be implemented as the function of the input of the back command, so that the step of generating an intermediate file can be omitted to improve the execution efficiency. ">" Indicates that the output is redirected, and the thing should be displayed in the screen to the file.
The shell program shell program first acquires information about all processes in the system and puts it in the temporary file TT1. Then let the program wait for 20 seconds (time can be adjusted according to the specific situation). Then get all the information of all processes again, and put all the fields into the temporary file TT2. At this time, two temporary files are compared, find those processes that are different from 20 seconds before and after (where there is a process that has changed the CPU time). Remove ">" and "<" generated during comparison, and store the results in the temporary file TT3. Then sort the contents in TT3, and the front-rear information of the same process that is consumed by the CPU time changes together.
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 ========================= co That is OK!
RM TT1 TT2 TT3
The above Grep '^' is information that is used to remove some intermediate command processes generated when the SHELL program is executed. Use CUT to delete the process information in each row, making the output clearer. GREP -V 0:00 used to remove the process before and after 20 seconds. In this way, the process of consuming the CPU time before and after 20 seconds is displayed on the screen, which can easily find a process of problems. Finally, in order not to leave useless junk files in the system, three temporary files are all deleted.