Linux system management-process management-process view (top, ps, pstree commands)

Preface

The program that is running, the program in the system needs one or more processes to ensure that the program can run in the system. So as long as any program runs, it will generate one or more processes (such as a program like apache). What is
process management? ? ? Is it just to end the process? ? ? No
analysis:
(1) Check the server health status (look at the CPU and memory usage) over 80%. Human intervention is needed to determine which process is occupying which resources. If MySQL occupies too many resources, it means The pressure on mysql is very high, and MySQL query optimization needs to be done. There may be some problems with the program. The server pressure is relatively high. If optimization is done, the process pressure is still so high, indicating that this requires the use of better computers, the need to add memory, or build a cluster of servers to meet the needs of the work. If the resources occupied by the normal process are high, it means that the capacity of the computer is insufficient and a better computer is needed.
(2) If it is an illegal process, an incorrect process, if it is only a less commonly used process, it should be terminated. If the program is judged to be an illegal process, it cannot be terminated directly. I found a process I haven't seen before, if I kill it directly, if it is really a virus, a Trojan horse, after a while, or next time I restart it, I will restart it. The correct approach: Judge what the process is, find the main body of the virus and Trojan, and then use manual or anti-virus software to completely clear it before you can terminate such a process. Regardless of whether the process occupying resources is a normal process, it cannot be terminated directly.
Conclusion:
So the job of killing the process is actually the least commonly used job in process management , mainly to judge the health of the server.
(1) All processes are properly terminated. If the virtual machine process is not killed directly, it is directly closed in the program. When do you need to kill the process,When the means of normal termination of the process fails, this time will consider killing the process.
(2) The main task of process management is to judge the health of the server.
The ideal operation and maintenance engineer, in an ideal state, the server does not crash, and the service does not crash. I have found that my server is in a sub-healthy state. Through human intervention, the server can recover from the crashed state. The operation and maintenance engineer is the most The main job is to judge health.
(3) It is necessary to build a special monitoring server, which can manage multiple servers. It is more convenient to manage, and of course it is more troublesome than process management commands. If you kill the correct process, it may cause the server to crash. Even if the service is not needed, you should first try to terminate it correctly. service httpd stop to stop the service, if it does not take effect, consider killing it.

1. Introduction to the process

A process is a program or command that is being executed. Each process is a running entity, has its own address space, and occupies certain system resources.

2. The role of process management

Determine the health status of the server (mainly)
View all processes in the system
Kill processes

3. View all processes in the system (ps)

Command: ps aux
The role of the option: a View the foreground process x View the background process u Show that this process is generated by the user
: View all processes in the system, using the BSD operating system format
We are more used to ps aux (common usage)

Command: ps -le(这个是unix的格式)
The function of the option: -l displays more detailed information, -e displays all processes.
Function: View all processes in the system, using the Linux standard command format.
Insert picture description here
Init process is the first process called when Linux starts. Is he in the
TTY column of the parent process of other processes ? It means that this process is not called by the terminal, but directly generated by the kernel. I don't know which terminal came from.

3.1 The meaning of the first line

The content of the first line meaning
USER Who caused the process
PID ID of the process
%CPU The percentage of CPU resources occupied by the process, the higher the occupation, the more resources the process consumes
%MEM The percentage of physical memory occupied by the process, the higher the occupation, the more resources the process consumes
VSZ The size of the virtual memory occupied by the process, in KB
RSS The actual physical memory occupied by the process, in KB
TTY In which terminal the process is running, tty1-tty7 represents the local console terminal, tty1-tty6 represents the local character interface terminal, and tty7 is the graphic terminal. pts/0-255 represents virtual terminal
STAT Process status. Common states are: R: running, S: sleeping, T: stopped, s: including child processes, +: in the background
START The start time of the process
TIME This process occupies CPU computing time, please note that it is not system time. (The larger the time occupied by TIME, the more resources are consumed)
COMMAND The name of the command that spawned this process

4. View system health status

Command: top [Options]
Options:
-d Number of seconds: Specify the top command to update every few seconds. The default is 3 seconds.
Commands that can be executed in the interactive mode of the top command
:? Or h: Display the help of interactive mode.
P: Sort by CPU usage, the default is this item
M: Sort by memory usage
N: Sort by PID
q: Quit top
Insert picture description here
This thing is very similar to task manager, task manager is It is managed every second, and Linux is updated every three seconds. The main work of this command is the first five lines. Help you tell you what health status your system is currently in. How long the server has been running, and the longer the server has been running, will reflect that you are a better administrator from the side. Is to execute the top command, and then take a screenshot. The sun is generally more than 300 days.

4.1 The first line of information is the task queue information

content Description
12:26:46 System current time
up 1 day,13:32 The running time of the system, the machine has been running for 1 day, 13 hours and 32 minutes
2 users Two users are currently logged in
load average:0.00,0.00,0.00 The system has an average load of 1 minute, 5 minutes, and 15 minutes. Generally, when it is less than 1, the load is small. If it is greater than 1, the system has been overloaded

How to judge your load pressure? It is based on the number of cores of the CPU. If the CPU is single-core, this is more than 1, indicating that you are still very stressed five minutes ago. By analogy,
if it is four-core or eight-core, if it is eight-core, then More than eight will be considered overloaded.
Because there is no program running now, the pressure is relatively small.

4.2 The second line of progress information

content Description
Tasks:95total The total number of processes in the system
1 running Number of running processes
94 sleeping Sleep process
0 stopped Stopping process
0 zombie Zombie process. If it is not 0, you need to manually check the zombie process

Zombie process: After the child process code is executed, the process that is waiting for the parent process to reclaim resources is a zombie process.
Zombie process: This process is being terminated, but it has not been terminated completely. If there are one or two zombie processes, don't intervene first. It is possible that the service is stopping, and the top command caught it before it stopped completely. Look at it after a while, under normal circumstances this zombie process disappears. If it does not disappear, it proves that an error was reported during the termination of the process, it was stuck, and it was not terminated correctly. This is the time to manually judge, there is no problem, you need to manually terminate.

4.3 The third line CPU information

content Description
Cpu(s):0.1%us Percentage of CPU occupied by user mode
0.1% sy Percentage of CPU occupied by system mode
0.0% ni Percentage of CPU occupied by user processes that have changed their priority
99.7%id CPU percentage of idle CPU
0.1% wa Percentage of CPU used by processes waiting for input/output
0.0%hi Percentage of CPU occupied by hard interrupt request service
0.1% and Percentage of CPU occupied by soft interrupt request service
0.0%st st (Steal time) virtual time percentage. When there is a virtual machine, the percentage of time that the virtual CPU waits for the actual CPU

The CPU information is mainly the idle rate of the CPU. If the idle rate is very low, such as less than 20%, the server is running under high pressure, and human intervention is required. If there is no illegal process, you need to add memory or buy a server to build a cluster. Up.

4.4 The fourth line of physical memory information

content Description
Mem:625344k total The total amount of physical memory, in KB
571504k used The amount of physical memory used
53840k free The amount of free physical memory. When we use the virtual machine, a total of 628MB of memory is allocated, so there is only 53MB of free memory.
65800k buffers The amount of memory used as buffer

The total size of physical memory, how much has been used, how much is free, and how much is buffering

4.5 The fifth line of exchange (swap) information

content Description
Swap:524280k total Total size of swap partition (virtual memory)
0k used The size of the interactive partition that has been used
524280k free The size of the free swap partition
409280k cached The size of the swap partition as a cache

No more than 2G, otherwise it will just waste hard disk space

4.6 Small summary

The key points of the top command are: the
first line: load average; the
third line: the idle rate of the CPU; the
fourth line: the idleness of the memory.
If these three indicators are too low, it proves that the server is under too much pressure and requires human intervention. Top is to check the health status.
The content below the first five lines is very similar to the output of ps. It is sorted by the percentage of CPU usage by default. Use Shift + m to sort by memory usage. The top command or the task manager in Windows is more resource-intensive. Yes, just look at it, don’t open it all the time when it’s okay.

5. View the process tree

Command: pstree [选项]
Options:
-p: display the PID of the process
-u: display the user of the process

Insert picture description here

tree is to view the directory tree, pstree is to view the process tree,
init is the parent process of all processes, and the underlying process depends on the parent process.

Guess you like

Origin blog.csdn.net/weixin_46818279/article/details/107977108