Home >Web Front-end >PS Tutorial >A 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
-Cc21d5b2dbf788dd07eb92dd26e84b7aa Lists the status of the specified command
- -linese1bb43da1dc495f3a2319be5fe7b6df5 Number of lines displayed per page
--widthf2c936634e508bbe9dc7919058f624c1 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 '-'? 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 '(cron|syslog)' 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中文网!