Home > Operation and Maintenance > Linux Operation and Maintenance > What is the command to query processes under Linux?

What is the command to query processes under Linux?

青灯夜游
Release: 2022-04-13 16:41:40
Original
22802 people have browsed it

Commands for querying processes: 1. ps command, which can view detailed information of all running processes in the system, with the syntax "ps aux" or "ps -le"; 2. top command, which can monitor the running status of processes in real time , the syntax is "top option"; 3. The pstree command can view the process tree and display the relationship between the program and the process in a tree structure.

What is the command to query processes under Linux?

#The operating environment of this tutorial: CentOS 6 system, Dell G3 computer.

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

Whether you are a Linux system administrator or an ordinary user, monitoring the running status of system processes and terminating some out-of-control processes in a timely manner is a daily routine.

Although commands are used for process management in Linux, the main purpose of process management is the same, that is, to view the programs and processes running in the system, determine the health status of the server, and forcefully terminate unnecessary processes.

So what is the command to query the process under Linux? The following article will share with you some Linux query process commands.

Linux ps command: View running processes

ps command is the most commonly used command to monitor processes. You can use this command to view the system Detailed information about all running processes in .

The basic format of the ps command is as follows:

[root@localhost ~]# ps aux
#查看系统中所有的进程,使用 BS 操作系统格式
[root@localhost ~]# ps -le
#查看系统中所有的进程,使用 Linux 标准命令格式
Copy after login

Options:

  • a: Display all processes of a terminal, except the session leader;

  • u: Displays the user belonging to the process and memory usage;

  • x: Displays processes that do not control the terminal;

  • -l: Display more detailed information in long format;

  • -e: Display all processes;

can be viewed Yes, the ps command is a little different. Some of its options cannot include "-", such as the command "ps aux", where "aux" is an option, but "-" cannot be included in front of it.

If you execute the "man ps" command, you will find that the help of the ps command has many available formats to adapt to different UNIX-like systems, which is inconvenient to remember. Therefore, I suggest that you just memorize a few fixed options. For example:

  • "ps aux" can view all processes in the system;

  • "ps -le" can view all processes in the system , and you can also see the PID and process priority of the parent process of the process;

  • "ps -l" can only see the processes generated by the current shell;

These three commands are enough. Let’s check them out separately.

[Example 1]

[root@localhost ~]# ps aux
Copy after login
#查看系统中所有的进程
USER PID %CPU %MEM  VSZ  RSS   TTY STAT START TIME COMMAND
root   1  0.0  0.2 2872 1416   ?   Ss   Jun04 0:02 /sbin/init
root   2  0.0  0.0    0    0   ?    S   Jun04 0:00 [kthreadd]
root   3  0.0  0.0    0    0   ?    S   Jun04 0:00 [migration/0]
root   4  0.0  0.0    0    0   ?    S   Jun04 0:00 [ksoftirqd/0]
…省略部分输出…
Copy after login

Table 1 lists the specific meanings of each column in the above output information.

##USER Which user created this process. PIDThe ID of the process. %CPUThe percentage of CPU resources occupied by the process. The higher the occupied percentage, the more resources the process consumes. %MEMThe percentage of physical memory occupied by the process. The higher the percentage occupied, the more resources the process consumes. VSZThe size of the virtual memory occupied by this process, in KB. RSSThe actual physical memory size occupied by this process, in KB. TTYWhich terminal is this process running on? Among them, tty1 ~ tty7 represent local console terminals (different terminals can be switched through the Alt F1 ~ F7 shortcut keys), tty1 ~ tty6 are local character interface terminals, and tty7 is a graphical terminal. pts/0 ~ 255 represents a virtual terminal, usually a remote connection terminal. The first remote connection occupies pts/0, and the second remote connection occupies pts/1, which increases in sequence. STATProcess status. Common states are as follows: STARTThe start time of this process. TIMEThis process takes up CPU computing time, please note that it is not system time. COMMANDThe command name that generates this process.

【例 2】"ps aux"命令可以看到系统中所有的进程,"ps -le"命令也能看到系统中所有的进程。由于 "-l" 选项的作用,所以 "ps -le" 命令能够看到更加详细的信息,比如父进程的 PID、优先级等。但是这两个命令的基本作用是一致的,掌握其中一个就足够了。

[root@localhost ~]# ps -le
F S UID PID PPID C  PRI Nl ADDR  SZ WCHAN TTY      TIME  CMD
4 S   0   1    0 0  80   0 -    718 -     ?    00:00:02  init
1 S   0   2    0 0  80   0 -      0 -     ?    00:00:00  kthreadd
1 S   0   3    2 0 -40   - -      0 -     ?    00:00:00  migration/0
1 S   0   4    2 0  80   0 -      0 -     ?    00:00:00  ksoflirqd/0
1 S   0   5    2 0 -40   - -      0 -     ?    00:00:00  migration/0
…省略部分输出…
Copy after login

表 2 罗列出以上输出信息中各列的含义。

Table 1 ps command output information meaning
HeaderMeaning
    -D: A sleep state that cannot be awakened, usually used in I/O situations.
  1. -R: The process is running.
  2. -S: The process is in sleep state and can be awakened.
  3. -T: Stop state, which may be suspended in the background or the process is in a debug state.
  4. -W: Memory interaction state (invalid starting from 2.6 kernel).
  5. -X: Dead process (should not appear).
  6. -Z: Zombie process. The process has been terminated, but part of the program is still in memory.
  7. -<: High priority (the following status occurs in BSD format).
  8. -N: Low priority.
  9. -L: Locked into memory.
  10. -s: Contains child processes.
  11. -l: Multithreading (lowercase L).
  12. -: Located in the background.
表 2 ps -le 命令输出信息
表头含义
F进程标志,说明进程的权限,常见的标志有两个:
  • 1:进程可以被复制,但是不能被执行;
  • 4:进程使用超级用户权限;
S进程状态。具体的状态和"psaux"命令中的 STAT 状态一致;
UID运行此进程的用户的 ID;
PID进程的 ID;
PPID父进程的 ID;
C该进程的 CPU 使用率,单位是百分比;
PRI进程的优先级,数值越小,该进程的优先级越高,越早被 CPU 执行;
NI进程的优先级,数值越小,该进程越早被执行;
ADDR该进程在内存的哪个位置;
SZ该进程占用多大内存;
WCHAN该进程是否运行。"-"代表正在运行;
TTY该进程由哪个终端产生;
TIME该进程占用 CPU 的运算时间,注意不是系统时间;
CMD产生此进程的命令名;

【例 3】如果不想看到所有的进程,只想查看一下当前登录产生了哪些进程,那只需使用 "ps -l" 命令就足够了:

[root@localhost ~]# ps -l
#查看当前登录产生的进程
F S UID   PID  PPID C PRI NI ADDR SZ WCHAN TTY       TIME CMD
4 S 0   18618 18614 0  80  0 - 1681  -     pts/1 00:00:00 bash
4 R 0   18683 18618 4  80  0 - 1619  -     pts/1 00:00:00 ps
Copy after login

可以看到,这次从 pts/1 虚拟终端登录,只产生了两个进程:一个是登录之后生成的 Shell,也就是 bash;另一个是正在执行的 ps 命令。

Linux top命令:持续监听进程运行状态

ps 命令可以一次性给出当前系统中进程状态,但使用此方式得到的信息缺乏时效性,并且,如果管理员需要实时监控进程运行情况,就必须不停地执行 ps 命令,这显然是缺乏效率的。

为此,Linux 提供了 top 命令。top 命令可以动态地持续监听进程地运行状态,与此同时,该命令还提供了一个交互界面,用户可以根据需要,人性化地定制自己的输出,进而更清楚地了进程的运行状态。

top 命令的基本格式如下:

[root@localhost ~]#top [选项]
Copy after login

选项:

  • -d 秒数:指定 top 命令每隔几秒更新。默认是 3 秒;

  • -b:使用批处理模式输出。一般和"-n"选项合用,用于把 top 命令重定向到文件中;

  • -n 次数:指定 top 命令执行的次数。一般和"-"选项合用;

  • -p 进程PID:仅查看指定 ID 的进程;

  • -s:使 top 命令在安全模式中运行,避免在交互模式中出现错误;

  • -u 用户名:只监听某个用户的进程;

在 top 命令的显示窗口中,还可以使用如下按键,进行一下交互操作:

  • ? 或 h:显示交互模式的帮助;

  • P:按照 CPU 的使用率排序,默认就是此选项;

  • M:按照内存的使用率排序;

  • N:按照 PID 排序;

  • T:按照 CPU 的累积运算时间排序,也就是按照 TIME+ 项排序;

  • k:按照 PID 给予某个进程一个信号。一般用于中止某个进程,信号 9 是强制中止的信号;

  • r:按照 PID 给某个进程重设优先级(Nice)值;

  • q:退出 top 命令;

我们看看 top 命令的执行结果,如下:

[root@localhost ~]# top
top - 12:26:46 up 1 day, 13:32, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 95 total, 1 running, 94 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.1%us, 0.1%sy, 0.0%ni, 99.7%id, 0.1%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 625344k total, 571504k used, 53840k free, 65800k buffers
Swap: 524280k total, 0k used, 524280k free, 409280k cached
PID   USER PR NI VIRT  RES  SHR S %CPU %MEM   TIME+ COMMAND
19002 root 20  0 2656 1068  856 R  0.3  0.2 0:01.87 top
1     root 20  0 2872 1416 1200 S  0.0  0.2 0:02.55 init
2     root 20  0    0    0    0 S  0.0  0.0 0:00.03 kthreadd
3     root RT  0    0    0    0 S  0.0  0.0 0:00.00 migration/0
4     root 20  0    0    0    0 S  0.0  0.0 0:00.15 ksoftirqd/0
5     root RT  0    0    0    0 S  0.0  0.0 0:00.00 migration/0
6     root RT  0    0    0    0 S  0.0  0.0 0:10.01 watchdog/0
7     root 20  0    0    0    0 S  0.0  0.0 0:05.01 events/0
8     root 20  0    0    0    0 S  0.0  0.0 0:00.00 cgroup
9     root 20  0    0    0    0 S  0.0  0.0 0:00.00 khelper
10    root 20  0    0    0    0 S  0.0  0.0 0:00.00 netns
11    root 20  0    0    0    0 S  0.0  0.0 0:00.00 async/mgr
12    root 20  0    0    0    0 S  0.0  0.0 0:00.00 pm
13    root 20  0    0    0    0 S  0.0  0.0 0:01.70 sync_supers
14    root 20  0    0    0    0 S  0.0  0.0 0:00.63 bdi-default
15    root 20  0    0    0    0 S  0.0  0.0 0:00.00 kintegrityd/0
16    root 20  0    0    0    0 S  0.0  0.0 0:02.52 kblockd/0
17    root 20  0    0    0    0 S  0.0  0.0 0:00.00 kacpid
18    root 20  0    0    0    0 S  0.0  0.0 0:00.00 kacpi_notify
Copy after login

我们解释一下命令的输出。top 命令的输出内容是动态的,默认每隔 3 秒刷新一次。命令的输出主要分为两部分:

  • 第一部分是前五行,显示的是整个系统的资源使用状况,我们就是通过这些输出来判断服务器的资源使用状态的;

  • 第二部分从第六行开始,显示的是系统中进程的信息;

Linux pstree命令:查看进程树

pstree 命令是以树形结构显示程序和进程之间的关系,此命令的基本格式如下:

[root@localhost ~]# pstree [选项] [PID或用户名]
Copy after login

表 1 罗列出了 pstree 命令常用选项以及各自的含义。

表 1 pstree命令常用选项及含义
选项含义
-a显示启动每个进程对应的完整指令,包括启动进程的路径、参数等。
-c不使用精简法显示进程信息,即显示的进程中包含子进程和父进程。
-n根据进程 PID 号来排序输出,默认是以程序名排序输出的。
-p显示进程的 PID。
-u显示进程对应的用户名称。

需要注意的是,在使用 pstree 命令时,如果不指定进程的 PID 号,也不指定用户名称,则会以 init 进程为根进程,显示系统中所有程序和进程的信息;反之,若指定 PID 号或用户名,则将以 PID 或指定命令为根进程,显示 PID 或用户对应的所有程序和进程。

init 进程是系统启动的第一个进程,进程的 PID 是 1,也是系统中所有进程的父进程。

【例 1】

[root@1ocalhost ~]# pstree
init──┬──abrc-dump-oopa
├──abrtd
├──acpid
...省略部分输出...
├──rayslogd───3*[{rsyslogrd}]
#有3个rsyslogd进程存在
├──sshd───sshd───bash───pstree
#Pstree命令进程是在远程连接中被执行的
├──udevd───2*[udevd]
└──xinecd
Copy after login

【例 2】如果想知道某个用户都启动了哪些进程,使用 pstree 命令可以很容易实现,以 mysql 用户为例:

[root@1ocalhost ~]# pstree mysql
mysqid---6*[{mysqid}]
Copy after login

此输出结果显示了 mysql 用户对应的进程为 mysqid,并且 mysqid 进程拥有 5 个子进程(外加 1 个父进程,共计 6 个进程)。

Linux lsof命令:列出进程调用或打开的文件信息

我们知道,通过 ps 命令可以查询到系统中所有的进程,那么,是否可以进一步知道这个进程到底在调用哪些文件吗?当然可以,使用 lsof 命令即可。

lsof 命令,“list opened files”的缩写,直译过来,就是列举系统中已经被打开的文件。通过 lsof 命令,我们就可以根据文件找到对应的进程信息,也可以根据进程信息找到进程打开的文件。

lsof 命令的基本格式如下:

[root@localhost ~]# lsof [选项]
Copy after login

此命令常用的选项及功能,如表 1 所示。

表 1 lsof 命令常用选项及功能
选项功能
-c 字符串只列出以字符串开头的进程打开的文件。
+d 目录名列出某个目录中所有被进程调用的文件。
-u 用户名只列出某个用户的进程打开的文件。
-p pid列出某个 PID 进程打开的文件。

【例 1】

[root@localhost ~]# lsof | more
#查询系统中所有进程调用的文件
COMMAND PID  USER  FD    TYPE  DEVICE  SIZE/OFF  NODE  NAME
init               1      root    cwd  DIR    8,3       4096        2           /
init               1      root    rtd    DIR    8,3       4096        2           /
init               1      root    txt     REG   8,3       145180    130874 /sbin/init
init               1      root    mem REG   8,3       142472    665291 /lib/ld-2.12.so
init               1      root    mem REG   8,3       58704      655087 /lib/libnss_files-2.12.so
…省略部分输出…
Copy after login

这个命令的输出非常多。它会按照 PID,从 1 号进程开始列出系统中所有的进程正在调用的文件名。

【例 2】

[root@localhost ~]# lsof /sbin/init
#查询某个文件被哪个进程调用
COMMAND PID USER  FD  TYPE  DEVICE  SIZE/OFF  NODE    NAME
init               1     root    txt  REG   8,3      145180     130874   /sbin/init
Copy after login

lsof 命令也可以反过来查询某个文件被哪个进程调用。这个例子就查询到 /sbin/init 文件是被 init 进程调用的。

相关推荐:《Linux视频教程

The above is the detailed content of What is the command to query processes under Linux?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template