Linux: ps command

本文介绍了如何使用ps命令来管理进程、查看详细信息及检测内存泄漏,包括运行状态、用户、PID、命令、内存使用等关键信息。通过实例展示了如何利用ps命令的不同选项,如按用户名、PID、PPID、命令名称等筛选进程,并通过排序功能发现潜在的内存泄漏问题。

About ps

Reports a snapshot of the status of currently running processes.


Syntax

ps [options]

Description

On every UNIX-like operating system, the process status command (ps) displays information about active processes. Every operating system’s version of ps is slightly different, so consult your own documentation for specific options.

This documentation describes a version of ps common to many distributions of Linux. It accepts several kinds of options:
1) UNIX options, which may be grouped and must be preceded by a dash (“-“).
2) BSD options, which may be grouped and must not be used with a dash.
3) GNU long options, which are preceded by two dashes (“–”).


Examples

List Currently Running Processes (ps -ef, ps -aux)

Its a commonly used example with a ps command to list down all the process which are currently running in a machine. The following example shows the options of ps command to get all the processes.

$ ps -ef
root     26551     5  0 Feb10 ?        00:03:41 [pdflush]
root     26570     5  0 Feb10 ?        00:00:20 [pdflush]
root     30344  3382  0 Feb21 ?        00:00:11 sshd: root@pts/14
root     30365 30344  0 Feb21 pts/14   00:00:02 -bash
root     30393  3382  0 Feb21 ?        00:00:10 sshd: root@pts/15

Where:
-e to display all the processes.
-f to display full format listing.

In case of BSD machines, you can use ‘ps -aux’ will give the details about all the process as shown above.

$ ps -aux

List the Process based on the UID and Commands (ps -u, ps -C)

Use -u option to displays the process that belongs to a specific username. When you have multiple username, separate them using a comma. The example below displays all the process that are owned by user wwwrun, or postfix.

$ ps -f -u wwwrun,postfix
UID        PID  PPID  C STIME TTY          TIME CMD
postfix   7457  7435  0 Mar09 ?        00:00:00 qmgr -l -t fifo -u
wwwrun    7495  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7496  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7497  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7498  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7499  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun   10078  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun   10082  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
postfix  15677  7435  0 22:23 ?        00:00:00 pickup -l -t fifo -u

Often ps is used with grep like “ps -aux | grep command” to get the list of process with the given command.

But ps command itself has an option to achieve the same. The following example shows that all the processes which has tatad.pl in its command execution.

$ ps -f -C tatad.pl
UID        PID  PPID  C STIME TTY          TIME CMD
root      9576     1  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9577  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9579  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9580  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9581  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9582  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root     12133  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl

Note: We can create aliases for ps command to list processes based on commands, users or groups.


List the processes based on PIDs or PPIDs (ps -p, ps –ppid)

Each process will be assigned with the unique Process ID (PID).

When you launch some application, it might fork number of processes and each sub process will have its own PID. So, each process will have its own process id and parent processid.

For all the processes that a process forks will have the same PPID (parent process identifier). The following method is used to get a list of processes with a particular PPID.

$ ps -f --ppid 9576
UID        PID  PPID  C STIME TTY          TIME CMD
root      9577  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9579  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9580  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9581  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root      9582  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl
root     12133  9576  0 Mar09 ?        00:00:00 /opt/tata/perl/bin/perl /opt/tata/bin/tatad.pl

The following example is to list the processes which has given PID.

$ ps -f  -p 25009,7258,2426
UID        PID  PPID  C STIME TTY          TIME CMD
root      2426     4  0 Mar09 ?        00:00:00 [reiserfs/0]
root      7258     1  0 Mar09 ?        00:00:00 /usr/sbin/nscd
postfix  25009  7435  0 00:02 ?        00:00:00 pickup -l -t fifo -u

List Processes in a Hierarchy (ps –forest)

The example below display the process Id and commands in a hierarchy. –forest is an argument to ps command which displays ASCII art of process tree. From this tree, we can identify which is the parent process and the child processes it forked in a recursive manner.

$ ps -e -o pid,args --forest
  468  \_ sshd: root@pts/7
  514  |   \_ -bash
17484  \_ sshd: root@pts/11
17513  |   \_ -bash
24004  |       \_ vi ./790310__11117/journal
15513  \_ sshd: root@pts/1
15522  |   \_ -bash
 4280  \_ sshd: root@pts/5
 4302  |   \_ -bash

Note: You can also use tree and pstree command to displays process in a nice tree structure.


List elapsed wall time for processes (ps -o pid,etime=)

If you want the get the elapsed time for the processes which are currently running ps command provides etime which provides the elapsed time since the process was started, in the form [[dd-]hh:]mm:ss.

The below command displays the elapsed time for the process IDs 1 (init) and process id 29675.

For example “10-22:13:29″ in the output represents the process init is running for 10days, 22hours,13 minutes and 29seconds. Since init process starts during the system startup, this time will be same as the output of the ‘uptime’ command.

# ps -p 1,29675 -o pid,etime=
  PID
    1 10-22:13:29
29675  1-02:58:46

List all threads for a particular process (ps -L)

You can get a list of threads for the processes. When a process hangs, we might need to identify the list of threads running for a particular process as shown below.

 $ ps -C java -L -o pid,tid,pcpu,state,nlwp,args
  PID   TID %CPU S NLWP COMMAND
16992 16992  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16993  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16994  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16995  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16996  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16997  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16998  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 16999  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17000  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17001  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17002  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17003  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 17024  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 15753  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006
16992 15754  0.0 S   15 ../jre/bin/java -Djava.ext.dirs=../jre/lib/ext:../lib:../auto_lib -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5006

-L option is used to display the list of threads for a process which has the command given. And it also displays nlwp, which represents number of light weight processes. In the above example, a total of 15 java threads are running.


Finding memory Leak (ps –sort pmem)

A memory leak, technically, is an ever-increasing usage of memory by an application.

With common desktop applications, this may go unnoticed, because a process typically frees any memory it has used when you close the application.

However, In the client/server model, memory leakage is a serious issue, because applications are expected to be available 24×7. Applications must not continue to increase their memory usage indefinitely, because this can cause serious issues. To monitor such memory leaks, we can use the following commands.

$ ps aux --sort pmem
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  1520  508 ?        S     2005   1:27 init
inst  1309  0.0  0.4 344308 33048 ?      S     2005   1:55 agnt (idle)
inst  2919  0.0  0.4 345580 37368 ?      S     2005  20:02 agnt (idle)
inst 24594  0.0  0.4 345068 36960 ?      S     2005  15:45 agnt (idle)
root 27645  0.0 14.4 1231288 1183976 ?   S     2005   3:01 /TaskServer/bin/./wrapper-linux-x86-32

In the above ps command, –sort option outputs the highest %MEM at bottom. Just note down the PID for the highest %MEM usage. Then use ps command to view all the details about this process id, and monitor the change over time. You had to manually repeat it or put it as a cron to a file.

$ ps ev --pid=27645
PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
27645 ? S 3:01 0 25 1231262 1183976 14.4 /TaskServer/bin/./wrapper-linux-x86-32

$ ps ev --pid=27645
PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
27645 ? S 3:01 0 25 1231262 1183976 14.4 /TaskServer/bin/./wrapper-linux-x86-32

Note: In the above output, if RSS (resident set size, in KB) increases over time (so would %MEM), it may indicate a memory leak in the application.


Reference

http://www.thegeekstuff.com/2011/04/ps-command-examples/
http://www.computerhope.com/unix/ups.htm

<think>我们正在处理一个用户问题:adb error unknown command ps fix 用户在使用ADB时遇到了'unknown command ps'错误,需要解决方案。 首先,我们需要理解这个错误。ADB(Android Debug Bridge)是一个命令行工具,允许开发者与Android设备通信。当用户输入`adb ps`时,系统返回“unknown command ps”,这是因为ADB本身没有`ps`命令。 但是,用户可能想列出设备上的进程,这通常通过`adb shell ps`来实现。因为`ps`是一个在Android设备的shell中运行的命令,而不是ADB的直接命令。 因此,解决方案是使用`adb shell`进入设备的命令行界面,然后运行`ps`,或者直接运行`adb shell ps`。 步骤: 1. 确保设备已连接并且ADB可以识别设备:运行`adb devices`来检查设备列表。 2. 然后运行`adb shell ps`来获取进程列表。 注意:在较新的Android版本中,`ps`命令可能不可用,或者需要特定的参数。替代方案是使用`adb shell top`或者`adb shell procrank`(如果设备支持)。另外,从Android 7.0开始,推荐使用`adb shell cmd activity processes`来查看进程信息。 如果用户是在Windows命令提示符下运行,可能会遇到另一个问题:命令提示符本身有`ps`命令吗?实际上,在Windows中,原生的命令提示符没有`ps`命令,所以用户可能混淆了环境。 因此,我们给出以下解决方案: 解决方案: 1. 使用`adb shell`进入设备shell,然后运行`ps`(如果可用)。 2. 或者直接运行`adb shell ps`(注意:在某些设备上可能不可用,因为`ps`可能不在默认路径中,或者被精简掉了)。 3. 如果`ps`不可用,可以尝试使用其他命令,如`adb shell top -n 1`(显示一次进程列表)或`adb shell procrank`(需要设备有root权限)。 4. 对于较新的Android版本,使用`adb shell cmd activity processes`。 另外,用户可能误输入了命令。ADB的常用命令包括`install`、`uninstall`、`push`、`pull`等,而`ps`不是ADB的直接命令。 因此,我们建议用户使用正确的命令格式。 如果用户仍然遇到问题,可能是ADB版本过旧,建议更新ADB工具到最新版本。 最后,我们生成相关问题,以帮助用户进一步学习。</think>### ADB "unknown command ps" 错误解决方案 当在 ADB 中输入 `ps` 命令出现 `unknown command ps` 错误时,这是因为 `ps` 不是 ADB 的直接命令,而是需要在 Android 设备的 shell 环境中执行的命令。以下是完整解决方案: #### 1. **正确执行命令格式** `ps` 命令必须通过 `adb shell` 执行: ```bash adb shell ps ``` 或先进入交互式 shell 再执行: ```bash adb shell $ ps # 在设备 shell 中执行 ``` #### 2. **替代命令(当 `ps` 不可用时)** 某些 Android 系统精简了 `ps`,可尝试: - 使用 `top` 查看实时进程: ```bash adb shell top -n 1 ``` - 使用 `procrank`(需 root 权限): ```bash adb shell procrank ``` - 通过 `dumpsys` 获取进程信息: ```bash adb shell dumpsys activity processes ``` #### 3. **检查 ADB 环境** - **更新 ADB 工具**:旧版本可能导致兼容性问题,从 [Android 开发者官网](https://developer.android.com/studio/releases/platform-tools) 下载最新版 - **验证设备连接**: ```bash adb devices # 确认设备列在列表中 ``` - **重启 ADB 服务**: ```bash adb kill-server adb start-server ``` #### 4. **权限问题处理** - 部分设备需要开启 **USB 调试**(开发者选项 → USB 调试) - 执行 `ps -A` 等命令可能需要 root 权限: ```bash adb root # 重启 adbd 为 root 模式 adb shell ps ``` #### 5. **跨平台注意事项** - 在 **Windows PowerShell** 中,`ps` 是系统保留命令(进程列表),需明确区分: ```powershell adb shell ps # 正确:adb 的 shell 参数 ``` > **根本原因**:`ps` 是 Linux/Android 的系统进程查看工具,不属于 ADB 命令集。ADB 仅作为通信桥梁,实际命令需在设备 shell 中执行[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值