首页 » 翻译 » 正文

10个基本的linux ps 命令例子

估计linux下常用的命令里ps算是使用率较高的了,今天来翻译一下这篇ps相关的文章

Linux ps command

The ps command on linux is one of the most basic commands for viewing the processes running on the system. It provides a snapshot of the current processes along with detailed information like user id, cpu usage, memory usage, command name etc. It does not display data in real time like top or htop commands. But even though being simpler in features and output it is still an essential process management/monitoring tool that every linux newbie should know about and learn well.

PS命令是linux下最基本的命令,它用来查看系统中正在运行的进程。它提供了当前的线程以及相关的详细信息,例如用户id,cpu占用,内存占用,启动的命令等。它和top或者htop不一样的地方在于它不是实时的。但它的输出简单,所以它是每一个运维人员需要掌握的命令

In this post we are going to revise the basics of using the ps command to check the processes and filter and sort them in different ways to suit better.

在本文中,我们将讲一下如何使用PS来查看进城以及相关的过滤以及排序

Note on syntax

PS的语法:

The ps command comes with an unusual set of 2 syntax styles. That is BSD and UNIX both. New users are often confused with and mis-interpret the two styles. So here is some basic info to get it clear before moving on.

Ps命令又两种语法格式,分别是BSD风格和UNIX风格,新用户常常感觉到困扰。所以这里我们简单说一下基本的知识然后继续

注意: ps aux 和ps -aux 并不相同,举例来说 -u 是显示进程的拥有着,但是u 表示显示详细信息

BSD style – The options in bsd style syntax are not preceded with a dash.

BSD: 就是后边没有-,直接跟选项

UNIX/LINUX style – The options in linux style syntax are preceded by a dash as usual.

UNIX/LINUX:就是后边跟着 – 然后跟选项

How to use ps command

1. Display all processes

显示所有进程

The following command will give a full list of processes

如下的两个命令会显示所有的进程:

Pipe the output to “less” to make it scrollable.

使用 管道链接 less命令后我们可以使用滚动条来查看具体信息

Use the “u” option or “-f” option to display detailed information about the processes

使用u 或者-f 来显示详细信息

2. Display process by user

显示某用户的所有进程

To filter the processes by the owning user use the “-u” option followed by the username. Multiple usernames can be provided separated by a comma.

来单独显示某用户拥有的进程 -u “username”,可以输入过个用户名,通过”,”分割

3. Show process by name or process id

显示指定名称的进程(或者进程id)

To search the processes by their name or command use the “-C” option followed by the search term.

我们可以通过-C 来查找具体名称的进程

To display processes by process id, use the “-p” option and provides the process ids separated by comma.

可以通过-p 选项来显示具体id大进程信息,也可以通过“,”来一次传入多个

The “-C” must be provided with the exact process name and it cannot actually search with a partial name or wildcard. To search the process list more flexibly, the usual grep command has to be used

-C 选项要求我们要输入完全匹配的进程名称,所以我们更倾向于使用grep来进行筛选

4. Sort process by cpu or memory usage

按照cpu或者内存进行排序

System administrators often want to find out processes that are consuming lots of memory or CPU. The sort option will sort the process list based on a particular field or parameter.

系统管理员常常需要查找到消耗CPU或者内存的罪魁祸首,排序选项让我们可以按照指定的参数进行排序

Multiple fields can be specified with the “–sort” option separated by a comma. Additionally the fields can be prefixed with a “-” or “+” symbol indicating descending or ascending sort respectively. There are lots of parameters on which the process list can be sorted. Check the man page for the complete list.

多个参数的时候我们可以使用 –sort来进行排序,通过“,”来输入,我们可以在排序的参数前边家“-”或者“+”,表示整序或者倒序进行排序,更多的选项可以通过man手册来查找

Display the top 5 processes consuming most of the cpu.

显示占用cpu最高的5个进程

5. Display process hierarchy in a tree style

通过进程树的形式来显示进程

Many processes are actually forked out of some parent process, and knowing this parent child relationship is often helpful. The ‘–forest’ option will construct an ascii art style tree view of the process hierarchy.

许多进程其实通过许多父进程fork出来的,并且很多时候知道这些父子关系很又用, –forest 可以实现这个目的

The following command will search for processes by the name apache2 and construct a tree and display detailed information.

如下的命令会寻找apache2进程,并且通过tree的形式来显示信息

6. Display child processes of a parent process

显示自进程:

Here is an example of finding all forked apache processes.

下面的例子是查找所有apache的子进程:

The first process that is owned by root is the main apache2 process and all other apache2 processes have been forked out of this main process. The next command lists all child apache2 processes using the pid of the main apache2 process

第一个进程是root拥有的,所以事你主进程,然后所有其它的apache2进程都是通过这个进程fork出来的,下面的命令显示了所有apache2的进程通过主进程的id

7. Display threads of a process

显示进程下的所有线程

The “-L” option will display the threads along with the processes. It can be used to display all threads of a particular process or all processes.

-L选项可以显示所有的线程,可以用来显示指定进程下的线程

The following command shall display all the threads owned by the process with id 3150.

下面的命令会显示进程3150下所有的线程

8. Change the columns to display

指定要显示的选项

The ps command can be configured to show a selected list of columns only. There are a large number of columns to to show and the full list is available in the man pages.

ps命令可以指定我们要显示的数据栏,ps有很多栏目可以显示,具体的可以参考man 手册

The following command shows only the pid, username, cpu, memory and command columns.

下面的命令只会显示 pid ,username,cpu,memory ,和comm

It is possible to rename the column labels

我们也可以重命名这些栏目

Quite flexible.

9. Display elapsed time of processes

显示进程的运行时间

The elapsed time indicates, how long the process has been running for. The column for elapsed time is not shown by default, and has to be brought in using the “-o” option

默认,进程的运行时间是默认不显示的,如果想查看,必须使用-o选项

10. Turn ps into an realtime process viewer

将ps转换成实时显示的进程监控器

As usual, the watch command can be used to turn ps into a realtime process reporter. Simple example is like this

通常情况下,我们可以使用watch命令来让我们的ps信息实时显示,简单的例如下:

The output on my desktop is something like this.

输出结果如下:

The output would be updated every 1 second to refresh the stats. However do not think that this is similar to top.

输出结果每1妙更新一次,这个和top还是有区别的
You would notice that the output of top/htop command changes much more frequently compared to the above ps command.
This is because the top output sorts on a value that is a mix of cpu usage and memory usage. But the above ps command sorts in a more simpler manner, taking 1 column at a time (like school maths). So it would not update rapidly like top.

你会发现 top/htop命令的刷新频率比ps的高很多

这是因为top命令的输出是按照cpu和内存综合排序进行输出的,但是我们上面的ps命令是一个简单的输出,所以它不会和top一样

原文地址:http://www.binarytides.com/linux-ps-command/

Zhiming Zhang

Senior devops at Appannie
一个奔跑在运维路上的胖子
Zhiming Zhang

Latest posts by Zhiming Zhang (see all)