search
HomeWeb Front-endPS TutorialA Linux command every day: ps command

The ps command in Linux is the abbreviation of Process Status. The ps command is used to list the processes currently running on the system. The ps command lists the snapshots of the current processes, which are the processes at the moment when the ps command is executed. If you want to dynamically display process information, you can use the top command.

To monitor and control the process, you must first understand the situation of the current process, that is, you need to view the current process, and the ps command is the most basic and very powerful process viewing command. Use this command to determine which processes are running and their running status, whether the process has ended, whether the process has died, which processes are occupying too many resources, and so on. In short, most of the information can be obtained by executing this command.

ps provides us with a one-time view of the process, but the view results it provides are not dynamic and continuous; if you want to monitor the process time, you should use the top tool.

kill command is used to kill the process.


There are 5 states of processes on Linux:

1. Running (running or waiting in the run queue)

2. Interrupt (sleeping, blocked, waiting for a certain condition to form or receive a signal)

3. Uninterruptible (no wake-up and no runnability after receiving a signal, the process must wait until an interrupt occurs)

4. Zombie (the process has terminated, but the process descriptor exists until the parent process calls the wait4() system call and is released)

5. Stop (the process receives SIGSTOP, SIGSTP, SIGTIN, SIGTOU signals Stop running after running)


The 5 status codes of the ps tool identifying the process:

D uninterruptible sleep (usually IO)

R run runnable (on run queue)

S ​​interrupt sleeping

T stop traced or stopped

Z zombie a defunct ("zombie") process


1. Command format:

ps[parameter]

2. Command function:

Used to display the status of the current process

3. Command parameters:

a Display all processes

-a Display all programs under the same terminal

-A Display all processes

c Display the true status of the process Name

-N Reverse selection

-e Equal to "-A"

e Display environment variables

f Display the relationship between programs

-H Display tree structure

r Display the processes of the current terminal

T Display all programs of the current terminal

u All processes of the specified user

-au Displays more detailed information

-aux Displays all schedules that include other users

-C Lists the status of the specified command

- -lines Number of lines displayed per page

--width Number of characters displayed per page

--help Display help information

--version Display version display

4. Usage example:

Example 1: Display all process information

Command:

ps -A

Output:

[root@localhost test6]# ps -A
  PID TTY          TIME CMD
    1 ?        00:00:00 init
    2 ?        00:00:01 migration/0
    3 ?        00:00:00 ksoftirqd/0
    4 ?        00:00:01 migration/1
    5 ?        00:00:00 ksoftirqd/1
    6 ?        00:29:57 events/0
    7 ?        00:00:00 events/1
    8 ?        00:00:00 khelper
   49 ?        00:00:00 kthread
   54 ?        00:00:00 kblockd/0
   55 ?        00:00:00 kblockd/1
   56 ?        00:00:00 kacpid
  217 ?        00:00:00 cqueue/0
  ……省略部分结果

Description:

Example 2: Display specified user information

Command:

ps -u root

Output:

[root@localhost test6]# ps -u root
  PID TTY          TIME CMD
    1 ?        00:00:00 init
    2 ?        00:00:01 migration/0
    3 ?        00:00:00 ksoftirqd/0
    4 ?        00:00:01 migration/1
    5 ?        00:00:00 ksoftirqd/1
    6 ?        00:29:57 events/0
    7 ?        00:00:00 events/1
    8 ?        00:00:00 khelper
   49 ?        00:00:00 kthread
   54 ?        00:00:00 kblockd/0
   55 ?        00:00:00 kblockd/1
   56 ?        00:00:00 kacpid
    ……省略部分结果

Description:

Example 3: Display all process information, along with the command line

Command:

ps -ef

Output:

[root@localhost test6]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Nov02 ?        00:00:00 init [3]                  
root         2     1  0 Nov02 ?        00:00:01 [migration/0]
root         3     1  0 Nov02 ?        00:00:00 [ksoftirqd/0]
root         4     1  0 Nov02 ?        00:00:01 [migration/1]
root         5     1  0 Nov02 ?        00:00:00 [ksoftirqd/1]
root         6     1  0 Nov02 ?        00:29:57 [events/0]
root         7     1  0 Nov02 ?        00:00:00 [events/1]
root         8     1  0 Nov02 ?        00:00:00 [khelper]
root        49     1  0 Nov02 ?        00:00:00 [kthread]
root        54    49  0 Nov02 ?        00:00:00 [kblockd/0]
root        55    49  0 Nov02 ?        00:00:00 [kblockd/1]
root        56    49  0 Nov02 ?        00:00:00 [kacpid]
……省略部分结果

Description:

Example 4: Commonly used combinations of ps and grep to find specific processes

Command:

ps -ef|grep ssh

Output:

[root@localhost test6]# ps -ef|grep ssh
root      2720     1  0 Nov02 ?        00:00:00 /usr/sbin/sshd
root     17394  2720  0 14:58 ?        00:00:00 sshd: root@pts/0 
root     17465 17398  0 15:57 pts/0    00:00:00 grep ssh

Instructions:

Example 5: List the PID and related information that currently belongs to you for this login

Command:

ps -l

Output:

[root@localhost test6]# ps -l
F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0 17398 17394  0  75   0 - 16543 wait   pts/0    00:00:00 bash
4 R     0 17469 17398  0  77   0 - 15877 -      pts/0    00:00:00 ps

Description:

The meaning of each related information:

F represents the flag of this program, 4 represents the user as super user

S ​​represents the status (STAT) of this program. The meaning of each STAT will be introduced in the text.

UID The program is owned by the UID.

PID is the ID of this program. !

PPID is the ID of its superior parent program

The percentage of resources used by C CPU

PRI is the abbreviation of Priority (Priority Execution Order), which will be introduced in detail later

NI This is the Nice value. We will continue to introduce it in the next section.

ADDR This is the kernel function, indicating which part of the memory the program is in. If it is a running program, it is usually "-"

SZ The memory size used

WCHAN Is the program currently running? If it is -, it means it is running

TTY The login's terminal location

TIME The CPU time used.

Why is the command issued by CMD


By default, ps will only list the PID related to the current bash shell. So, when I use ps -l, there are only three PIDs.


实例6:列出目前所有的正在内存当中的程序

命令:

ps aux

输出:

[root@localhost test6]# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  10368   676 ?        Ss   Nov02   0:00 init [3]                  
root         2  0.0  0.0      0     0 ?        S<   Nov02   0:01 [migration/0]
root         3  0.0  0.0      0     0 ?        SN   Nov02   0:00 [ksoftirqd/0]
root         4  0.0  0.0      0     0 ?        S<   Nov02   0:01 [migration/1]
root         5  0.0  0.0      0     0 ?        SN   Nov02   0:00 [ksoftirqd/1]
root         6  0.0  0.0      0     0 ?        S<   Nov02  29:57 [events/0]
root         7  0.0  0.0      0     0 ?        S<   Nov02   0:00 [events/1]
root         8  0.0  0.0      0     0 ?        S<   Nov02   0:00 [khelper]
root        49  0.0  0.0      0     0 ?        S<   Nov02   0:00 [kthread]
root        54  0.0  0.0      0     0 ?        S<   Nov02   0:00 [kblockd/0]
root        55  0.0  0.0      0     0 ?        S<   Nov02   0:00 [kblockd/1]
root        56  0.0  0.0      0     0 ?        S<   Nov02   0:00 [kacpid]
……省略部分结果

说明:

USER:该 process 属于那个使用者账号的

PID :该 process 的号码

%CPU:该 process 使用掉的 CPU 资源百分比

%MEM:该 process 所占用的物理内存百分比

VSZ :该 process 使用掉的虚拟内存量 (Kbytes)

RSS :该 process 占用的固定的内存量 (Kbytes)

TTY :该 process 是在那个终端机上面运作,若与终端机无关,则显示 ?,另外, tty1-tty6 是本机上面的登入者程序,若为 pts/0 等等的,则表示为由网络连接进主机的程序。

STAT:该程序目前的状态,主要的状态有

R :该程序目前正在运作,或者是可被运作

S :该程序目前正在睡眠当中 (可说是 idle 状态),但可被某些讯号 (signal) 唤醒。

T :该程序目前正在侦测或者是停止了

Z :该程序应该已经终止,但是其父程序却无法正常的终止他,造成 zombie (疆尸) 程序的状态

START:该 process 被触发启动的时间

TIME :该 process 实际使用 CPU 运作的时间

COMMAND:该程序的实际指令


实例7:列出类似程序树的程序显示

命令:

ps -axjf

输出:

[root@localhost test6]# ps -axjf
Warning: bad syntax, perhaps a bogus &#39;-&#39;? See /usr/share/doc/procps-3.2.7/FAQ
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
    0     1     1     1 ?           -1 Ss       0   0:00 init [3]                  
    1     2     1     1 ?           -1 S<       0   0:01 [migration/0]
    1     3     1     1 ?           -1 SN       0   0:00 [ksoftirqd/0]
    1     4     1     1 ?           -1 S<       0   0:01 [migration/1]
    1     5     1     1 ?           -1 SN       0   0:00 [ksoftirqd/1]
    1     6     1     1 ?           -1 S<       0  29:58 [events/0]
    1     7     1     1 ?           -1 S<       0   0:00 [events/1]
    1     8     1     1 ?           -1 S<       0   0:00 [khelper]
    1    49     1     1 ?           -1 S<       0   0:00 [kthread]
   49    54     1     1 ?           -1 S<       0   0:00  \_ [kblockd/0]
   49    55     1     1 ?           -1 S<       0   0:00  \_ [kblockd/1]
   49    56     1     1 ?           -1 S<       0   0:00  \_ [kacpid]

说明:

实例8:找出与 cron 与 syslog 这两个服务有关的 PID 号码

命令:

输出:

[root@localhost test6]# ps aux | egrep &#39;(cron|syslog)&#39;
root      2682  0.0  0.0  83384  2000 ?        Sl   Nov02   0:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5
root      2735  0.0  0.0  74812  1140 ?        Ss   Nov02   0:00 crond
root     17475  0.0  0.0  61180   832 pts/0    S+   16:27   0:00 egrep (cron|syslog)
[root@localhost test6]#

说明:

其他实例:

1. 可以用 | 管道和 more 连接起来分页查看

命令:

ps -aux |more


2. 把所有进程显示出来,并输出到ps001.txt文件

命令:

ps -aux > ps001.txt


3. 输出指定的字段

命令:

 ps -o pid,ppid,pgrp,session,tpgid,comm

输出:

[root@localhost test6]# ps -o pid,ppid,pgrp,session,tpgid,comm
  PID  PPID  PGRP  SESS TPGID COMMAND
17398 17394 17398 17398 17478 bash
17478 17398 17478 17398 17478 ps
[root@localhost test6]#


更多每天一个linux命:ps命令相关文章请关注PHP中文网!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Photoshop for creating social media graphics?How do I use Photoshop for creating social media graphics?Mar 18, 2025 pm 01:41 PM

The article details using Photoshop for social media graphics, covering setup, design tools, and optimization techniques. It emphasizes efficiency and quality in graphic creation.

How do I prepare images for web use in Photoshop (file size, resolution, color space)?How do I prepare images for web use in Photoshop (file size, resolution, color space)?Mar 13, 2025 pm 07:28 PM

Article discusses preparing images for web use in Photoshop, focusing on optimizing file size, resolution, and color space. Main issue is balancing image quality with quick loading times.

How do I calibrate my monitor for accurate color in Photoshop?How do I calibrate my monitor for accurate color in Photoshop?Mar 13, 2025 pm 07:31 PM

Article discusses calibrating monitors for accurate color in Photoshop, tools for calibration, effects of improper calibration, and recalibration frequency. Main issue is ensuring color accuracy.

How do I use Photoshop's Content-Aware Fill and Content-Aware Move tools effectively?How do I use Photoshop's Content-Aware Fill and Content-Aware Move tools effectively?Mar 13, 2025 pm 07:35 PM

Article discusses using Photoshop's Content-Aware Fill and Move tools effectively, offering tips on selecting source areas, avoiding mistakes, and adjusting settings for optimal results.

How do I use Photoshop's video editing capabilities?How do I use Photoshop's video editing capabilities?Mar 18, 2025 pm 01:37 PM

The article explains how to use Photoshop for video editing, detailing steps to import, edit, and export videos, and highlighting key features like the Timeline panel, video layers, and effects.

How do I create animated GIFs in Photoshop?How do I create animated GIFs in Photoshop?Mar 18, 2025 pm 01:38 PM

Article discusses creating and optimizing animated GIFs in Photoshop, including adding frames to existing GIFs. Main focus is on balancing quality and file size.

How do I prepare images for web using Photoshop (optimize file size, resolution)?How do I prepare images for web using Photoshop (optimize file size, resolution)?Mar 18, 2025 pm 01:35 PM

Article discusses optimizing images for web using Photoshop, focusing on file size and resolution. Main issue is balancing quality and load times.

How can I master advanced selection techniques in Photoshop (e.g., Select and Mask)?How can I master advanced selection techniques in Photoshop (e.g., Select and Mask)?Mar 12, 2025 pm 02:07 PM

This article details advanced Photoshop selection techniques. It emphasizes mastering the Select and Mask workspace, strategic tool use (Quick Selection, Object Selection, Lasso tools, Brush), Refine Edge settings, and efficient workflows including

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools