Analysis and Application of process management in Linux

Source: Internet
Author: User
Tags pkill
Directory: 1. programs and processes; 1.1 process categories; 1.2 process attributes; 1.3 parent and child processes; 2. process management; 2.1ps process monitoring tool; 2.1.1ps parameter description; 2.1.2ps application example; 2.2pgrep3; kill, killall, pkill, and xkill; 3.1kill3.

Directory:

    1. programs and processes;
    1.1 Process Classification;
    1.2 process attributes;
    1.3 parent and child processes;

    2. process management;
    2.1 ps process monitoring tool;
    2.1.1 ps parameter description;
    2.1.2 ps application example;
    2.2 pgrep

    3. kill, killall, pkill, and xkill tools for terminating processes;
    3.1 kill
    3.2 killall
    3.3 pkill
    3.4 xkill

    4. top tool for monitoring system tasks;
    4.1 top command usage and parameters;
    4.2 top applications;

    5. process priority: nice and renice;
    6. about this article;
    7. postscript;

1. programs 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, LinuxSir. the WWW server used by Org 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.

1.1 Process Classification;

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 Fedora or 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
Because the daemon is always running, the status of the Daemon is waiting for the request to be processed. For example, whether we access the httpd servers of LinuxSir. Org and LinuxSir. Org is running, waiting for the user to access, that is, waiting for the task to be processed.

1.2 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 );

1.3 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;

2. process management;

Linux process management is implemented through process management tools, such as ps, kill, and pgrep;

2.1 ps process 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;


2.1.1 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. display processes in the task format;
F process is displayed in a tree format;
A. display all processes of all users (including other users );
X displays the process of the uncontrolled terminal;
R: displays running processes;
Ww to avoid detailed parameter truncation;
Our common options are combination of aux or lax, and the application of parameter f;

Explanation of ps aux or lax output;

       
        
USER process owner; PID process ID; PPID parent process; % CPU usage percentage of CPU processes; % MEM memory usage; NICE value of NI process, large value, indicates that the CPU usage is relatively low; VSZ process virtual size; number of pages where RSS resides; wchan tty terminal IDSTAT process status D Uninterruptible 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 memory switching (starting from kernel 2.6 is invalid ); X dead processes (never seen before); Z botnets; <processes with higher priority N processes with 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; WCHAN is waiting for process resources; START process start time; TIME the CPU consumed by the process; COMMAND name and parameters;
       
2.1.2 ps application example;
Example 1: ps aux is the most commonly used
[Root @ localhost ~] # Ps-aux | more
You can use | the MPs queue is connected to more to view it by page;
[Root @ localhost ~] # Ps-aux> ps001.txt
[Root @ localhost ~] # More ps001.txt
This shows 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 httpd
  root 4187 0.0 1.3 24236 10272 ? Ss 11:55 0:00 /usr/sbin/httpd
  apache 4189 0.0 0.6 24368 4940 ? S 11:55 0:00 /usr/sbin/httpd
  apache 4190 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpd
  apache 4191 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpd
  apache 4192 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpd
  apache 4193 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpd
  apache 4194 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpd
  apache 4195 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpd
  apache 4196 0.0 0.6 24368 4932 ? S 11:55 0:00 /usr/sbin/httpd
  root 4480 0.0 0.0 5160 708 pts/3 R+ 12:20 0:00 grep httpd

Example 2: a friendly relationship between parent and child processes

  

[root@localhost ~]# ps auxf |grep httpd
  root 4484 0.0 0.0 5160 704 pts/3 S+ 12:21 0:00 \_ grep httpd
  root 4187 0.0 1.3 24236 10272 ? Ss 11:55 0:00 /usr/sbin/httpd
  apache 4189 0.0 0.6 24368 4940 ? S 11:55 0:00 \_ /usr/sbin/httpd
  apache 4190 0.0 0.6 24368 4932 ? S 11:55 0:00 \_ /usr/sbin/httpd
  apache 4191 0.0 0.6 24368 4932 ? S 11:55 0:00 \_ /usr/sbin/httpd
  apache 4192 0.0 0.6 24368 4932 ? S 11:55 0:00 \_ /usr/sbin/httpd
  apache 4193 0.0 0.6 24368 4932 ? S 11:55 0:00 \_ /usr/sbin/httpd
  apache 4194 0.0 0.6 24368 4932 ? S 11:55 0:00 \_ /usr/sbin/httpd
  apache 4195 0.0 0.6 24368 4932 ? S 11:55 0:00 \_ /usr/sbin/httpd
  apache 4196 0.0 0.6 24368 4932 ? S 11:55 0:00 \_ /usr/sbin/httpd

The f parameter is used here; the parent-child relationship is clear at a glance;

 

[NextPage]

 

2.2 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:
# Ps parameter option program name

Common parameters:
-L list the program name and process ID;
-The ID of the start of the o process;
-N indicates the ID of the process to terminate;

Example:

[root@localhost ~]# pgrep -lo httpd
4557 httpd
[root@localhost ~]# pgrep -ln httpd
4566 httpd
[root@localhost ~]# pgrep -l httpd
4557 httpd
4560 httpd
4561 httpd
4562 httpd
4563 httpd
4564 httpd
4565 httpd
4566 httpd
[root@localhost ~]# pgrep httpd
4557
4560
4561
4562
4563
4564
4565
4566


3. kill, killall, pkill, and xkill tools for terminating processes;

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 applying

Related Article

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.