-->
ps这个命令最常见的,我们经常用它来检索进程,但在我认真阅读过ps的man文档之前,我使用的最多的选项是aux,用grep匹配出特定的进程,然后再处理它,现在看来这种做法非常笨拙,呵呵!其实我们完全可以用pgrep快速的完成这一串操作。
今天暂不讲解pgrep的用法,先让 我们更多的了解一下ps,首先ps支持三种选项格式:unix options这种选项通常需要在其前加横杠-,多值可梱绑;BSD options 选项前不用加任何符号;GNU Long options即GNU长选项,选项前加两个横杠;这三种选项可以混用,在产生冲突时程序会提示。
ps选项分类:检索类、输出列格式控制、线程信息、其他信息,在此我只列举一些常用到的用法,其他选项在用到时可以查看手册页。
-e用于显示所有进程,以下是输出的一小部分:
[root@supersun.biz ~]#ps -e
PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 migration/0
3 ? 00:00:00 ksoftirqd/0
4 ? 00:00:00 watchdog/0
5 ? 00:00:00 events/0
域定义:
PID 进程ID
TTY 与进程关联的终端
TIME 进程使用CPU累计时间
CMD 执行文件的名称
[root@supersun.biz ~]#ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:31 ? 00:00:00 init [5]
root 2 1 0 08:31 ? 00:00:00 [migration/0]
root 3 1 0 08:31 ? 00:00:00 [ksoftirqd/0]
root 4 1 0 08:31 ? 00:00:00 [watchdog/0]
域定义:
UID 用户ID
C CPU利用率,以整数表示。
STIME 进程的启动时间
-F选项添加了进程使用内存方面的一些信息:
[root@supersun ~]# ps -eF
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 1 0 0 508 684 1 Nov29 ? 00:00:00 init [5]
root 2 1 0 0 0 0 Nov29 ? 00:00:00 [migration/0]
root 3 1 0 0 0 0 Nov29 ? 00:00:00 [ksoftirqd/0]
root 4 1 0 0 0 0 Nov29 ? 00:00:00 [watchdog/0]
root 5 1 0 0 0 1 Nov29 ? 00:00:00 [migration/1]
root 6 1 0 0 0 1 Nov29 ? 00:00:00 [ksoftirqd/1]
root 7 1 0 0 0 1 Nov29 ? 00:00:00 [watchdog/1]
SZ 进程用到的swap的量,这是一个粗略计算;
RSS 驻留内存大小
PSR 进程使用的处理器,在多处理器上可以体现出来,如下面的两个进程使用的不同的处理器(超线程的也算):
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 1 0 0 508 684 1 Nov29 ? 00:00:00 init [5]
root 2 1 0 0 0 0 Nov29 ? 00:00:00 [migration/0]
-L用于显示线程
[root@supersun.biz ~]#ps -eLf
UID PID PPID LWP C NLWP STIME TTY TIME CMD
root 1 0 1 0 1 08:31 ? 00:00:00 init [5]
root 2 1 2 0 1 08:31 ? 00:00:00 [migration/0]
root 2233 2228 2233 3 8 08:35 ? 00:04:50 /root/firefox/firefox-bin
root 2233 2228 2271 0 8 08:36 ? 00:00:00 /root/firefox/firefox-bin
root 2233 2228 2272 0 8 08:36 ? 00:00:01 /root/firefox/firefox-bin
root 2233 2228 2277 0 8 08:36 ? 00:00:00 /root/firefox/firefox-bin
root 2233 2228 2278 0 8 08:36 ? 00:00:00 /root/firefox/firefox-bin
root 2233 2228 2279 0 8 08:36 ? 00:00:00 /root/firefox/firefox-bin
LWP light weight process ID 可以称其为线程ID。
NLWP 进程中的线程数number of lwps (threads) in the process。
显示进程树
[root@supersun.biz ~]#ps -ejH
PID PGID SID TTY TIME CMD
1 1 1 ? 00:00:00 init
2 1 1 ? 00:00:00 migration/0
3 1 1 ? 00:00:00 ksoftirqd/0
4 1 1 ? 00:00:00 watchdog/0
5 1 1 ? 00:00:00 events/0
6 1 1 ? 00:00:00 khelper
7 1 1 ? 00:00:00 kthread
10 1 1 ? 00:00:00 kblockd/0
11 1 1 ? 00:00:00 kacpid
86 1 1 ? 00:00:00 cqueue/0
89 1 1 ? 00:00:00 khubd
SID 即session ID
F即flag,其值有:
1 forked but didn't exec
4 used super-user privileges
S即STAT,其值有:
D Uninterruptible sleep (usually IO)
R Running or runnable (on run queue)
S Interruptible sleep (waiting for an event to complete)
T Stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by its parent.
对于BSD选项产生的值:
< high-priority (not nice to other users)
N low-priority (nice to other users)
L has pages locked into memory (for real-time and custom IO)
s is a session leader
l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ is in the foreground process group
当然我们还可以定义ps的输出域,如:
[root@supersun ~]# ps -e -o pid
PID
1
2
3
也可以指定多个域:
[root@supersun.biz ~]#ps -e -o pid,cmd
PID CMD
1 init [5]
2 [migration/0]
3 [ksoftirqd/0]
4 [watchdog/0]
5 [events/0]
6 [khelper]
需要注意的是在提定域输出的时使就不要使用-f等定义域输出的选项,这样会有冲突。
使用进程名对进程进行检索:
[root@supersun.biz ~]#ps -C syslogd -F
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 1479 1 0 424 632 0 08:32 ? 00:00:00 syslogd -m 0
对输出进行排序:
[root@supersun.biz ~]#ps -e -o pid,rss,pcpu,cmd --sort pcpu,rss
PID RSS %CPU CMD
2 0 0.0 [migration/0]
3 0 0.0 [ksoftirqd/0]
4 0 0.0 [watchdog/0]
2507 35948 0.2 stardict
2206 2276 2.0 gnome-screensaver
2170 15248 2.2 /usr/libexec/wnck-applet --oaf-activate-iid=OAFIID:GNOME_Wncklet_Factory
2064 8328 2.4 /usr/libexec/gnome-settings-daemon
2084 10860 2.5 metacity --sm-client-id=default1
6434 3284 2.6 rdesktop -T192.168.1.177 - Terminal Server Client -usunchao -rsound:off -
2213 22428 3.1 gnome-terminal
2233 104276 4.3 /root/firefox/firefox-bin
因为输出太长,我截去了一些 ,还有与selinux有关的信息,检索一系列进程等等我就不再列举了,自己用到的时候看看手册吧。

发表评论