linux ps 命令
Processes are one of the main parts of the operations systems. All user side even kernel side operations are executed with the process. The process generally created, run, and killed. This life cycle of the process generally the same. During this life cycle, we may need to get more information about processes. ps command is the most used command to list and get information about processes.
流程是操作系统的主要部分之一。 所有用户端甚至内核端操作都与该进程一起执行。 该过程通常是创建,运行和终止的。 这个过程的生命周期大体相同。 在此生命周期中,我们可能需要获取有关流程的更多信息。 ps命令是最常用的命令,用于列出和获取有关进程的信息。
列出当前用户进程 (List Current User Processes)
The process can be listed without providing any option to the ps command. But this will only list current user’s processes and do not list another system, user, or root user processes.
可以列出该进程,而无需为ps命令提供任何选项。 但这只会列出当前用户的进程,而不会列出另一个系统,用户或root用户进程。
$ ps
BSD语法(BSD Syntax)
ps
command is a very universal tool. ps also used other Unix variant operations systems. BSD provides options without a dash. So there is generally no difference but knowing BSD Syntax is beneficial. The following example uses the BSD syntax.
ps
命令是一个非常通用的工具。 ps还使用了其他Unix变体操作系统。 BSD提供的选项没有破折号。 因此,通常没有区别,但是了解BSD语法是有益的。 以下示例使用BSD语法。
ps aux
列出所有进程 (List All Process)
In previous steps, we have listed the processes running on the Linux system just for the current users. But generally, we need to list all processes in a single shot. We can provide the -ax
options in order to list all processes.
在前面的步骤中,我们仅列出了当前用户在Linux系统上运行的进程。 但是通常,我们需要一次列出所有进程。 我们可以提供-ax
选项以列出所有进程。
$ ps -A
PID
show process IDPID
显示进程IDTTY
show running consoleTTY
显示运行控制台TIME
show the used CPU timeTIME
显示所用的CPU时间CMD
show the complete command the thread numberCMD
显示完整命令的线程号
将进程列为树(List Processes As Tree)
In the previous example, we have printed all processes in the list format. There is an alternative presentation format named Tree. Tree format is a hierarchical format which will provide visual information about parent and child relationship.
在前面的示例中,我们以列表格式打印了所有进程。 还有一种名为Tree的替代表示格式。 树格式是一种分层格式,它将提供有关父子关系的可视信息。
$ ps -A --forest
列出流程信息(List Process Info)
While listing processes we can print more information about the process. We will use -u
option for this detailed information.
在列出流程时,我们可以打印有关流程的更多信息。 我们将使用-u
选项获取此详细信息。
$ ps -A l
UID
show the User ID of processUID
显示进程的用户IDPID
shows the process IDPID
显示进程IDPPID
shows the Parent Process IDPPID
显示父进程IDPRI
shows Process nice valuePRI
显示出良好的Craft.io价值STAT
shows the current status of the processSTAT
显示流程的当前状态TTY
shows the current console number if connectedTTY
显示当前控制台编号(如果已连接)
仅列出特定的命名过程 (List Only Specific Named Process)
While listing processes we may need to filter according to process or command name. Here we will use -C
parameter and process name for filter operation.
在列出进程时,我们可能需要根据进程或命令名称进行过滤。 在这里,我们将使用-C
参数和进程名称进行过滤操作。
$ ps -C acpid
仅打印特定的PID过程 (Print Only Specific PID Process)
Another way to filter processes id according to their PID. We can filter by given their PID. We will use the -p
option and PID’s in order to filter. In this example, we will filter multiple processes according to their IP address.
根据进程的PID过滤进程ID的另一种方法。 我们可以根据给定的PID进行过滤。 我们将使用-p
选项和PID进行过滤。 在此示例中,我们将根据其IP地址过滤多个进程。
$ ps -p 1331,1773
仅打印特定的用户进程(Print Only Specific User Processes)
Another useful filtering mechanism is filtering processes according to their owners or users. We will use -u
option and usernames to filter. In this example, we want to filter username ismail
.
另一个有用的过滤机制是根据进程的所有者或用户过滤进程。 我们将使用-u
选项和用户名进行过滤。 在此示例中,我们要过滤用户名ismail
。
$ ps -u ismail
显示进程线程(Display Threads of Process)
As we know Linux provides threads to make processes more efficient. Threads are created under the processes and complete given work by the parent process. We will use --ppid
option in order to list child threads.
众所周知,Linux提供了使进程更高效的线程。 线程是在流程下创建的,并由父流程完成给定的工作。 我们将使用--ppid
选项来列出子线程。
$ ps --ppid 1331
根据Cpu使用情况对流程进行排序 (Sort Process According To Cpu Usage)
While list process by default they are sorted with their PID’s. There are alternatives to sort processes. We can sort processes according to t their current CPU usage with --sort=pcpu
option like below.
默认情况下,虽然列表处理将它们的PID排序。 有其他排序过程。 我们可以使用--sort=pcpu
选项根据进程当前的CPU使用率对其进行排序,如下所示。
$ ps -A --sort=pcpu
根据内存使用情况对进程进行排序 (Sort Process According To Memory Usage)
We can also sort processes according to their memory usage with --sort=pmem
command like below.
我们还可以使用--sort=pmem
命令根据进程的内存使用情况对其进行排序,如下所示。
$ ps -aux --sort=pcpu
运行Ps实时模式 (Run Ps Real Time Mode)
The default behavior of ps
command is running and exiting. ps
command can be run in real-time without exiting. This is the same as top command. We will use an external command named watch and provide the ps command. In this example, we will list processes in 2-second intervals.
ps
命令的默认行为是运行和退出。 ps
命令可以实时运行而无需退出。 这与top命令相同。 我们将使用名为watch的外部命令并提供ps命令。 在此示例中,我们将以2秒为间隔列出进程。
$ watch -n 2 ps
翻译自: https://www.poftut.com/linux-ps-command-tutorial-list-processes-examples/
linux ps 命令