ps
命令,简称过程状态,是一种命令行实用程序,用于 **显示或查看与在Linux系统中运行的流程相关的信息。正如我们都知道的那样,Linux是一个多任务和多处理系统,因此,多个流程可以同时运行,而不会相互影响。 ps命令列出了当前运行的流程以及它们的PID和其他属性。
ps 指令 没有论点
ps 命令无参数列出当前壳中的运行过程
1ps
Output The output consists of four columns
PID
- This is the unique process ID TTY
- This is the typeof terminal that the user is logged in to TIME
- This is the time in minutes and seconds that the process has been running CMD
- The command that launched the process
在不同格式中查看所有运行过程
To have a glance at all the running processes, execute the command below ps -A
Output or
ps -e
Output
查看与终端相关的流程
To view processes associated with the terminal run ps -T
Output
查看与终端无关的过程
To view all processes with the exception of processes associated with the terminal and session leaders execute ps -a
A session leader is a process that starts other processes Output
显示所有正在运行的进程
查看所有当前流程执行
1ps -ax
Output
-a
flag stands for all processes -x
will display all processes even those not associated with the current tty
显示所有 BSD 格式的流程
如果您希望以 BSD 格式显示进程,请执行
1ps au
或
1ps aux
Output
完成完整格式列表
查看完整格式列表运行
1ps -ef
或
1ps -eF
Output
过滤过程根据用户
如果您想列出与特定用户相关的流程,请使用u
旗,如图所示。
1ps -u user
例如
1ps -u jamie
Output
通过 thread 过程过滤
如果您想知道某个特定过程的线程,请使用 -L
旗,然后是 PID 例如
1ps -L 4264
Output
显示每个流程作为 root 运行
有时,您可能想要披露由 root 用户运行的所有流程。
1ps -U root -u root
Output
显示组过程
如果您想要列出特定组运行相关的所有进程
1ps -fG group_name
或
1ps -fG groupID
例如
1ps -fG root
Output
搜索过程 PID
很可能通常不知道一个过程的PID,你可以通过运行搜索一个过程的PID。
1ps -C process_name
例如
1ps -C bash
Output
通过PID列出流程
您可以按其PID显示流程
1ps -fp PID
例如
1ps -fp 1294
Output
在树图中显示过程等级
通常情况下,大多数流程都是由父母的流程构成的。了解这个父母与子女的关系是非常有用的。下面的命令搜索流程以apache2的名称进行。
1ps -f --forest -C bash
Output
显示一个家长过程的儿童过程
例如,如果您想要显示所有属于 apache 的扭曲流程,请执行
1ps -o pid,uname,comm -C bash
Output The first process, which is owned by root is the main apache2 process and the rest of the processes have been forked from this main process To display all the child apache2 processes using the pid of the main apache2 process execute
1ps --ppid PID no.
例如
1ps --ppid 1294
Output
显示流程线索
ps 命令可以用来查看流程的线程,下面的命令显示了 PID pid_no 的所有流程的线程。
1ps -p pid_no -L
例如
1ps -p 1294 -L
Output
显示选定的列表列表
您可以使用 ps 命令仅显示您需要的列。
1ps -e -o pid,uname,pcpu,pmem,comm
The command above will only display the PID, Username, CPU, memory and command columns Output
重命名列标签
要重命名列标签,请执行下面的命令
1ps -e -o pid=PID,uname=USERNAME,pcpu=CPU_USAGE,pmem=%MEM,comm=COMMAND
Output
显示流程的过时时间
经过的时间是指该过程持续了多长时间
1ps -e -o pid,comm,etime
Output The -o option enables the column for elapsed time
使用 grep 的 ps 命令
ps 命令可与 grep 命令一起使用,以搜索特定过程。
1ps -ef | grep systemd
Output