Learn a Linux command in one minute - ps

foreword

Hello everyone, I am god23bin. Welcome to the " Learn a Linux Command in One Minute " series, it only takes one minute a day, and remembering a Linux command is not a problem . Today I'm going to talk about pscommands .

What is the ps command?

psThe full English name is process status, which means process status.

pscommand is a commonly used Linux command, used to view the process information running in the current system. It provides various details about the process, such as process ID (PID), process status, CPU usage, memory usage, running time, etc.

How to use the ps command?

psThe basic syntax of the command is as follows:

ps [选项参数]

Different types of process information can be obtained by adding different options.

Enter directly psto display the current process:

ps

The output contains 4 columns of information: PID, TTY, TIME, CMD

  • PID: (Process ID) unique process ID
  • TTY: (Teletypewriter) It refers to a terminal type or terminal device, an input and output device for a user to interact with a computer, such as a terminal window or a console.
  • TIME: The total time (in minutes and seconds) occupied by the CPU during the running of the process. The simple understanding is the time consumed by the CPU to run the process.
  • CMD: the name of the command that started the process

Sometimes when we execute psthe command , TIME is displayed as 00:00:00. This means that the process is not yet using the CPU, so there is no total CPU execution time.

For the above bash, this is the case, because bashit is just bashthe parent process of other processes that need to be run, so it does not use the CPU itself, and there is no CPU execution time.

The output above is actually not very useful as it doesn't contain much information. Generally we add parameters.

for example

Here are some examples of commonly used pscommands :

  1. 显示所有正在运行的进程:
ps -e

该命令等价于 ps -A,这里的 eA 分别是 everyall 的意思,所以等价。

  1. 显示与终端无关的所有进程:

我们打开一个新的终端,在新的终端中输入 man ps,接着我们回到原来的终端,输入以下命令:

ps -a

输出显示:

   PID TTY          TIME CMD
 23290 pts/1    00:00:00 man
 23301 pts/1    00:00:00 less
 23324 pts/0    00:00:00 ps

注意:pst/1 表示一个伪终端(pseudo-terminal),同理 pst/0 也是,0 和 1 只是终端的索引,当开启多个窗口时,将递增下去。

现在我们就可以在终端 0 看到终端 1 的进程了(man 命令和 less 命令的进程)

常用选项参数

以下是一些常用的 ps 命令选项参数:

  • -e:(every)显示所有进程,而不仅仅是当前用户的进程
  • -u:(user)显示面向用户的进程的详细信息,如进程所有者、CPU 使用率、内存使用率等
  • -f:(full-format)以全格式的列表显示进程的信息,包括进程之间的层级关系
  • -p:(pid)查找具有指定进程 ID 的进程
  • -C:(Command)查找具有指定命令名称的进程
  • -aux:以详细格式显示所有正在运行的进程,其中 x 指的是在没有控制终端的情况下列出进程,这些显示的主要是启动并运行在后台的进程

例子

  1. 显示所有进程信息:
ps -e

这将显示所有进程的信息,无论是当前用户的还是其他用户的。

  1. 显示进程的详细信息:
ps -u

该命令将显示进程的详细信息,包括进程所有者、CPU 使用率、内存使用率等。

可以看到输出更多列了,每列的意思是这样的:

  • USER:进程所有者的用户名
  • PID:进程的唯一标识符,即进程 ID
  • %CPU:进程使用的 CPU 资源的百分比
  • %MEM:进程使用的内存资源的百分比
  • VSZ:(Virtual Memory Size)进程的虚拟内存大小(以 KB 为单位)
  • RSS:(Resident Set Size)进程占用的物理内存大小(以 KB 为单位)
  • TTY:进程所关联的终端
  • STAT:进程的状态(例如,R 表示运行,S 表示睡眠,Z 表示僵尸(Zombie)等)
  • START:进程启动的时间
  • TIME:进程运行过程中占用 CPU 的总时间
  • COMMAND:启动进程的命令名称
  1. 显示进程之间的层级关系:
ps -f

  • UID:进程所有者的用户 ID
  • PID:进程的唯一 ID
  • PPID:(Parent Process ID)父进程的标识符
  • C:与 %CPU 的意思相同,进程使用的 CPU 资源的百分比,或者说进程的 CPU 使用率
  • STIME:与 START 的意思相同,进程启动的时间
  • TTY:与进程相关联的终端
  • TIME:进程运行过程中占用 CPU 的总时间
  • CMD:启动进程的命令名称

我们可以通过 -ef 来查看更多的进程的信息:

ps -ef

  1. 查找具有指定进程 ID 的进程:
ps -p PID

PID 替换为要查找的进程的实际进程 ID。这将显示具有指定进程 ID 的进程的信息。

比如现在我知道了一个进程的 PID 是 1333,想看详细信息,那么可以输入:

ps -p 1333 -f

  1. 查找具有指定命令名称的进程:
ps -C command_name

command_name 替换为要查找的进程的命令名称。这将显示具有指定命令名称的进程的信息。

ps 与其他命令一起使用

ps 可以通过管道符 | 和其他命令结合使用。

比如要将 ps 命令的输出,将这些输出通过管道交给 grep 命令进行匹配,找到我们想要的进程,起到一个过滤输出显示结果的作用,比如像找到属于 root 用户的进程,就可以输入:

ps -ef | grep root

想找到 bash 命令的进程,那么就可以输入:

ps -ef | grep bash

总结

ps 命令的使用,相信你通过这一分钟的学习已经掌握了,这是个很常用的命令,它有很多选项,但实际上,我们平常一般只用到 ps -aux 或者 ps -ef 来查看相关的正在运行的进程信息,或者当某些情况下想要找到某个进程,定位某个进程,就会使用 ps -ef | grep 进程名或者命令名 来进行匹配定位。

以上就是 ps 命令的基本内容,希望本文能够帮助大家更好地理解和应用 mvcp 命令。如果你对其他 Linux 命令也感兴趣,敬请期待我后续的文章。谢谢大家的阅读!

最后的最后

希望各位屏幕前的靓仔靓女们给个三连!你轻轻地点了个赞,那将在我的心里世界增添一颗明亮而耀眼的星!

咱们下期再见!

Guess you like

Origin juejin.im/post/7245922875182399548