天天看點

ps指令執行個體講解

檢索程序:ps

ps這個指令最常見的,我們經常用它來檢索程序,但在我認真閱讀過ps的man文檔之前,我使用的最多的選項是aux,用grep比對出特定的程序,然後再處理它,現在看來這種做法非常笨拙,呵呵!其實我們完全可以用pgrep快速的完成這一串操作。

今天暫不講解pgrep的用法,先讓我們更多的了解一下ps,首先ps支援三種選項格式:unix options這種選項通常需要在其前加橫杠-,多值可梱綁;BSD options 選項前不用加任何符号;GNU Long options即GNU長選項,選項前加兩個橫杠;這三種選項可以混用,在産生沖突時程式會提示。

ps選項分類:檢索類、輸出列格式控制、線程資訊、其他資訊,在此我隻列舉一些常用到的用法,其他選項在用到時可以檢視手冊頁。

-e用于顯示所有程序,以下是輸出的一小部分:

[[email protected] ~]#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 執行檔案的名稱

-f選項定義為full-format listing

[[email protected] ~]#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選項添加了程序使用記憶體方面的一些資訊:(測試了一下,該選項在AIX下是輸出格式定制選項,同-o選項。)

[[email protected] ~]# 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用于顯示線程(測試了一下, 在AIX下-L選項需要後跟PID才行。)

[[email protected] ~]#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。

顯示程序樹

[[email protected] ~]#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的輸出域,如:

[[email protected] ~]# ps -e -o pid

PID

1

2

3

也可以指定多個域:

[[email protected] ~]#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等定義域輸出的選項,這樣會有沖突。

使用程序名對程序進行檢索:

[[email protected] ~]#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

對輸出進行排序:

[[email protected] ~]#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 -rsoundff -

2213 22428 3.1 gnome-terminal

2233 104276 4.3 /root/firefox/firefox-bin

因為輸出太長,我截去了一些 ,還有與selinux有關的資訊,檢索一系列程序等等我就不再列舉了,自己用到的時候看看手冊吧。

作者:ixdba

來源:http://bbs.ixdba.net/redirect.php?tid=181&goto=lastpost

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

以下是AIX手冊中隊-o選項、-L選項的說明:

-o Format

Displays information in the format specified by the Format variable. Multiple field specifiers can be specified for

the Format variable. The Format variable is either a comma-separated list of field specifiers or a list of field

specifiers enclosed within a set of " " (double-quotation marks) and separated from one another by a comma or by one

or more spaces, or both.

Each field specifier has a default header. The default header can be overridden by appending an = (equal sign)

followed by the user-defined text for the header. The fields are written in the order specified on the command line in

column format. The field widths are specified by the system to be at least as wide as the default or user-defined

header text. If the header text is null, (such as if -o user= is specified), the field width is at least as wide as

the default header text. If all header fields are null, no header line is written.

The following field specifiers are recognized by the system:

args

Indicates the full command name being executed. All command-line arguments are included, though truncation may

occur. The default header for this field is COMMAND.

bnd

Indicates to which (if any) processor a process or kernel thread is bound. The default header for this field is

BND.

class

Indicates the workload managekment class assigned to the process. the default header for this field is CLASS.

comm

Indicates the short name of the command being executed. Command-line arguments are not included. The default

header for this field is COMMAND.

cpu

Determines process scheduling priority. CPU utilization of process or thread, incremented each time the system

clock ticks and the process or thread is found to be running. The value is decayed by the scheduler by dividing

it by 2 once per second. For the sched_other policy, Large values indicate a CPU intensive process and result

in lower process priority whereas small values indicate an I/O intensive process and result in a more favorable

priority.

etime

Indicates the elapsed time since the process started. The elapsed time is displayed in the following format:

[[ dd-]hh:]mm:ss

where dd specifies the number of days, hh specifies the number of hours, mm specifies the number of minutes,

and ss specifies the number of seconds. The default header for this field is ELAPSED.

group

Indicates the effective group ID of the process. The textual group ID is displayed. If the textual group ID

cannot be obtained, a decimal representation is used. The default header for this field is GROUP.

nice

Indicates the decimal value of the process nice value. The default header for this field is NI.

-o Format (Continued)

pcpu

Indicates the ratio of CPU time used to CPU time available, expressed as a percentage. The default header for

this field is %CPU.

pgid

Indicates the decimal value of the process group ID. The default header for this field is PGID.

pid

Indicates the decimal value of the process ID. The default header for this field is PID.

ppid

Indicates the decimal value of the parent process ID. The default header for this field is PPID.

rgroup

Indicates the real group ID of the process. The textual group ID is displayed. If the textual group ID cannot

be obtained, a decimal representation is used. The default header for this field is RGROUP.

ruser

Indicates the real user ID of the process. The textual user ID is displayed. If the textual user ID cannot be

obtained, a decimal representation is used. The default header for this field is RUSER.

scount

Indicates the suspend count for a kernel thread. The default header for this field is SC.

sched

Indicates the scheduling policy for a kernel thread. The default header for this field is SCH.

tag

Indicates the Workload Manager application tag. The default header for this field is TAG. The tag is a

character string up to 30 characters long and may be truncated when displayed by ps. For processes that do not

set their tag, this field displays as a - (hyphen).

tcpu

Total CPU time. Indicates the total accumulated CPU time for a single process. The default header for this

field is TCPU.

tctime

Total connect time. Indicates the total amount of time that a login session can be active. This is meaningful

only in the case of session leader processes. The default header for this field is TCTIME.

tdiskio

Total disk I/O. Indicates the total accumulated blocks of disk I/O for a single process. The default header for

this field is TDISKIO.

vmsize

Indicates the WLM virtual memory limits. When this is used, a new header, VMSIZ is displayed. VMSIZ displays

the virtual memory used by the process. This value is displayed in 1 MB units.

-o Format (Continued)

thcount

Indicates the number of kernel threads owned by the process. The default header for this field is THCNT.

THREAD

Indicates the following fields:

* User name (the uname field)

* Process and parent process IDs for processes (the pid and ppid fields)

* Kernel thread ID for threads (the tid field)

* The state of the process or kernel thread (the S field)

* The CPU utilization of the process or kernel thread (the C field)

* The priority of the process or kernel thread (the PRI field)

* The suspend count of the process or kernel thread (the scount field)

* The wait channel of the process or kernel thread (the WCHAN field)

* The flags of the process or kernel thread (the F field)

* The controlling terminal of the process (the tty field)

* The CPU to which the process or kernel thread is bound (the bnd field)

* The command being executed by the process (the comm field).

Threads are not actually displayed with the -o THREAD flag, unless the -m flag is also specified.

tid

Indicates the thread ID of a kernel thread. The default header for this field is TID.

time

Indicates the cumulative CPU time since the process started. The time is displayed in the following format:

[ dd-]hh:mm:ss

where dd specifies the number of days, hh specifies the number of hours, mm specifies the number of minutes,

and ss specifies the number of seconds. The default header for this field is TIME.

tty

Indicates the controlling terminal name of the process. The default header for this field is TT.

user

Indicates the effective user ID of the process. The textual user ID is displayed. If the textual user ID cannot

be obtained, a decimal representation is used. The default header for this field is USER.

vsz

Indicates, as a decimal integer, the size in kilobytes of the process in virtual memory. The default header for

this field is VSZ.

-o Format (Continued)

Otherwise, multiple fields in a specified format can be displayed by the Format variable, including field descriptors.

If field descriptors are used in the Format variable, it must be enclosed in double quotation marks (" "). The

following table shows how field descriptors correspond to field specifiers:

Field Field Default

Descriptors Specifiers Headers

%a args COMMAND

%c comm COMMAND

%t etime ELAPSED

%G group GROUP

%n nice NI

%C pcpu %CPU

%r pgid PGID

%p pid PID

%P ppid PPID

%g rgroup RGROUP

%u ruser RUSER

%x time TIME

%y tty TTY

%U user USER

%z vsz VSZ

-L pidlist

Generates a list of descendants of each and every pid that has been passed to it in the pidlist variable. The pidlist

variable is a list of comma-separated process IDs. The list of descendants from all of the given pid is printed in the

order in which they appear in the process table.

繼續閱讀