Linux Process Management (1), linux Process Management (
Process IntroductionPrograms and processes
A program is a software designed to complete a task. For example, OpenOffice is a program. What is a process? A process is a running program.
A running program may have multiple processes. For example, the WWW server used by self-taught it network is an apache server. When the Administrator starts the service, many people may access it. That is to say, many users request the httpd service at the same time, the apache server will create multiple httpd processes to serve them.
Process category
Processes are generally divided into three types: interaction process, batch processing process, and daemon process.
It is worth mentioning that the daemon process is always active and generally runs in the background. The daemon process is generally started by the system automatically activated by the script or by the super administrator root at startup. For example, in Redhat, we can define the running level of the startup script of the httpd server. This file is located in/etc/init. in the d directory, the file name is httpd,/etc/init. d/httpd is the daemon of the httpd server. When the running level is set to 3 and 5, it starts when the system starts.
[root@localhost ~]# chkconfig --level 35 httpd on
Process attributes:
Process ID (PID): A unique value used to differentiate processes;
The ID (PPID) of the parent process and the parent process );
The User ID (UID) of the Startup Process and the group (GID) of the startup process );
Process status: the status can be run R, sleep S, or zombie Z;
Priority of process execution;
The terminal name that the process is connected;
Process resource usage: for example, the resource usage (memory and CPU usage );
Parent and Child processes:
Their relationship is the relationship between management and management. When the parent process is terminated, the child process also terminates. But the child process is terminated, and the parent process is not necessarily terminated. For example, when the httpd server is running, we can kill its child processes. The parent process will not be terminated because of the termination of the child process.
In process management, when we find that a process that occupies too many resources or cannot be controlled, we should kill it to protect the stable and secure operation of the system;
Process Management
Linux Process Management is implemented through process management tools, such as ps, kill, and pgrep;
Ps Monitoring Tool
Ps provides a one-time View of the process. The results are not dynamically consecutive. If you want to monitor the process time, you should use the top tool;
Ps parameter description
Ps provides many option parameters, which are commonly used in the following scenarios;
L long format output; u displays processes in the username and start time order; j displays processes in the task format; f displays processes in the tree format; a: displays all processes of all users (including other users); x displays processes without control terminals; r displays processes in progress; ww avoids detailed parameter truncation;
Our common options are combination of aux or lax, and the application of parameter f;
Ps aux
Or
Lax
Output explanation;
USER process owner; PID process ID; PPID parent process; % CPU usage percentage of CPU processes; % MEM memory usage; PRI indicates the "executable priority" of the program. The NICE value of the NI process is large, indicating that the CPU usage is less; the virtual size of the vsz process; and the number of pages in which RSS reside; TTY terminal IDSTAT Process status D uninterrupted sleep (usually IO) R is running and can be crossed in the queue; S is in sleep state; T is stopped or tracked; W enters the memory switch (which is invalid since kernel 2.6); X dead processes (never seen); Z botnets; <process with a higher priority N process with a lower priority L some pages are locked into the memory; s process leader (with sub-processes under it); l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do) + process group in the background; Process resources waiting for WCHAN; START Process startup TIME; TIME process CPU consumption TIME; COMMAND name and parameters;
Ps application example;
Example 1: ps aux is the most commonly used
[Root @ localhost ~] # Ps-aux | more available | the MPs queue is connected to more and can be viewed by PAGE [root @ localhost ~] # Ps-aux> ps001.txt [root @ localhost ~] # More ps001.txt displays all the processes and outputs them to the ps001.txt file. Then, you can view them by page through more.
Example 2: Combine with grep to extract the process of the specified program;
[root@localhost ~]# ps aux | grep httpdroot 4187 0.0 1.3 24236 10272 ? Ss 11:55 0:00 /usr/sbin/httpdapache 4189 0.0 0.6 24368 4940 ? S 11:55 0:00 /usr/sbin/httpdapache 4190 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpdapache 4191 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpdapache 4192 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpdapache 4193 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpdapache 4194 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpdapache 4195 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpdapache 4196 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpdroot 4480 0.0 0.0 5160 708 pts/3 R+ 12:20 0:00 grep httpd
Pgrep
Pgrep is a tool used to query processes by program names. It is generally used to determine whether a program is running. In server configuration and management, this tool is often used and simple and clear;
Usage:
# Pgrep parameter option program name
Common Parameters
-L list the program name and process ID;-o process start ID;-n Process Termination ID;
Example:
[root@localhost ~]# pgrep -lo httpd4557 httpd[root@localhost ~]# pgrep -ln httpd4566 httpd[root@localhost ~]# pgrep -l httpd4557 httpd4560 httpd4561 httpd4562 httpd4563 httpd4564 httpd4565 httpd4566 httpd[root@localhost ~]# pgrep httpd45574560456145624563456445654566
Kill, killall, and pkill
Terminate a process or terminate a running program, usually through kill, killall, pkill, xkill, etc. For example, if a program is dead but cannot exit, you should consider using these tools.
In addition, in server management, you can use these tools to stop the parent process that does not involve database server programs. Why cannot the parent process of the database server be killed using these tools? The reason is simple. When these tools force terminate the database server, more file fragments will be generated for the database. When the fragmentation reaches a certain level, the database may crash. For example, it is best to close the mysql server according to its normal program, instead of using pkill mysqld or killall mysqld as dangerous actions. Of course, we should use kill to kill database sub-processes that occupy too much resources.