ps 程序的觀察。
pstree 顯示程序之間的相關性。
kill 程序的刪除。
killall 以指令名稱刪除程序。
[dywang@dywOffice ~]$ ps --help
********* simple selection ********* ********* selection by list *********
-A all processes -C by command name
-N negate selection -G by real group ID (supports names)
-a all w/ tty except session leaders -U by real user ID (supports names)
-d all except session leaders -g by session OR by effective group name
-e all processes -p by process ID
T all processes on this terminal -s processes in the sessions given
a all w/ tty, including other users -t by tty
g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
r only running processes U processes for specified users
x processes w/o controlling ttys t by tty
*********** output format ********** *********** long options ***********
-o,o user-defined -f full --Group --User --pid --cols --ppid
-j,j job control s signal --group --user --sid --rows --info
-O,O preloaded -o v virtual memory --cumulative --format --deselect
-l,l long u user-oriented --sort --tty --forest --version
-F extra full X registers --heading --no-heading --context
********* misc options *********
-V,V show version L list format codes f ASCII art forest
-m,m,-L,-T,H threads S children in sum -y change -l format
-M,Z security data c true command name -c scheduling class
-w,w wide output n numeric WCHAN,UID -H process hierarchy
參數:
****** 程序選擇 *******
無選項:僅列出目前 shell 下的程序
-A :所有的 process 均顯示出來,與 -e 具有同樣的效用;
-a :列出除了 session leadeers 外的所有與終端機有關的程序;
-d :列出除了 session leadeers 外的所有的程序。
a :列出所有與終端機有關的程序。
ax :等效 -A
-C :指定程序名稱。
-U :列出所有實際使用者啟動的程序。
-u :列出所有有效使用者有關的程序。
-t,t :列出某終端機有關的程序 (t 後面可不接 tty)。
實際使用者 (real user): 為實際程序的執行者,
有效使用者 (effective user): 主要用於校驗程序執行時的權限。
一舨而言,兩者是一樣的。但如執行 sudo username 時實際使用者是 username,但有效使用者卻是 root。\
****** 輸出格式 *******
u :將程序 user 放第一欄,經常與 ax 結合成 axu。若單獨 u 存在,則只列目前 user 啟動且與終端機有關的程序。
-l,l :長格式
****** 其他格式 *******
f :以 ASCII 樹狀輸出。
j :工作控制格式顯示。
ps 的參數多得離譜,而且有沒有加上 - 差很多。詳細的用法應參考 man ps。
#範例一:欲列出之程序的選擇
[dywang@dywOffice ~]$ ps
PID TTY TIME CMD
4790 pts/1 00:00:00 bash
14734 pts/1 00:00:00 ps
[dywang@dywOffice ~]$ ps -a
PID TTY TIME CMD
4201 tty1 00:00:00 startx
4217 tty1 00:00:00 xinit
4222 tty1 00:00:00 startkde
4242 tty1 00:00:39 gcin
4252 tty1 00:00:00 dbus-launch
4332 tty1 00:00:00 kwrapper
12538 pts/0 00:00:00 bash
25658 pts/2 00:00:00 bash
14735 pts/1 00:00:00 ps
[dywang@dywOffice ~]$ ps a
PID TTY STAT TIME COMMAND
4756 tty3 Ss+ 0:00 /sbin/mingetty tty3
4757 tty4 Ss+ 0:00 /sbin/mingetty tty4
4758 tty5 Ss+ 0:00 /sbin/mingetty tty5
4759 tty6 Ss+ 0:00 /sbin/mingetty tty6
29261 tty1 Ss 0:00 -bash
4201 tty1 S+ 0:00 /bin/sh /usr/bin/startx
### 中間省略 ###
4790 pts/1 Ss 0:00 /bin/bash
4890 tty2 Ss+ 0:00 -bash
12538 pts/0 S+ 0:00 -bash
9307 pts/2 Ss 0:00 su -
9308 pts/5 Ss+ 0:00 /bin/bash
25658 pts/2 S+ 0:00 -bash
14736 pts/1 R+ 0:00 ps a
[root@dywOffice ~]# ps -A
PID TTY TIME CMD
1 ? 00:00:26 init
2 ? 00:00:00 migration/0
3 ? 00:00:00 ksoftirqd/0
4 ? 00:00:00 migration/1
5 ? 00:00:00 ksoftirqd/1
### 中間省略 ###
9308 pts/5 00:00:00 bash
25658 pts/2 00:00:00 bash
25132 ? 00:00:00 mplayer
26581 ? 00:00:00 esd
26583 ? 00:00:11 stardict
26589 ? 00:00:00 bonobo-activati
2085 ? 00:00:00 kio_file
27006 ? 00:00:00 proftpd
27016 ? 00:00:00 kio_file
27017 ? 00:00:00 kio_pop3
27021 pts/0 00:00:00 ps
[root@dywOffice ~]# ps t tty1
PID TTY STAT TIME COMMAND
5034 tty1 Ss 0:00 -bash
5072 tty1 S+ 0:00 /bin/sh /usr/bin/startx
5088 tty1 S+ 0:00 xinit /usr/lib/X11/xinit/xinitrc
5118 tty1 S 0:00 /bin/sh /usr/bin/startkde
5138 tty1 S 0:02 gcin
5146 tty1 S 0:00 /usr/bin/dbus-launch
5223 tty1 S 0:00 kwrapper ksmserver
#範例二:以長格式列出目前 shell 下的程序
[root@linux ~]# ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 0 5881 5654 0 76 0 - 1303 wait pts/0 00:00:00 su
4 S 0 5882 5881 0 75 0 - 1349 wait pts/0 00:00:00 bash
4 R 0 6037 5882 0 76 0 - 1111 - pts/0 00:00:00 ps
# 上面這個資訊其實很多,各相關資訊的意義為:
# F 程序的旗標 (flag), 4 代表使用者為 super user;
# S 程序的狀態 (STAT),S 睡眠狀態,R 正在執行,詳 man page;
# PID 程序的 ID 。 PPID 為其父程序的 PID;
# C CPU 使用的資源百分比
# PRI Priority (優先執行序) 的縮寫,詳 man page;
# NI Nice 值。
# ADDR kernel function,指出該程序在記憶體的那個部分。若是 running
# 的程序,一般就是『 - 』。
# SZ 使用掉的記憶體大小;
# WCHAN 目前這個程序是否正在運作當中,若為 - 表示正在運作;
# TTY 登入者的終端機位置;
# TIME 使用掉的 CPU 時間。
# CMD 所下達的指令為何?
#範例三:以使用者導向列出所有的程序:
[root@linux ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 1740 540 ? S Jul25 0:01 init [3]
root 2 0.0 0.0 0 0 ? SN Jul25 0:00 [ksoftirqd/0]
root 3 0.0 0.0 0 0 ? S< Jul25 0:00 [events/0]
.....中間省略.....
root 5881 0.0 0.3 5212 1204 pts/0 S 10:22 0:00 su
root 5882 0.0 0.3 5396 1524 pts/0 S 10:22 0:00 bash
root 6142 0.0 0.2 4488 916 pts/0 R+ 11:45 0:00 ps aux
#範例四:列出類似程序樹的程序顯示:
[root@linux ~]# ps -axjf
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
0 1 0 0 ? -1 S 0 0:01 init [3]
1 2 0 0 ? -1 SN 0 0:00 [ksoftirqd/0]
.....中間省略.....
1 5281 5281 5281 ? -1 Ss 0 0:00 /usr/sbin/sshd
5281 5651 5651 5651 ? -1 Ss 0 0:00 \_ sshd: deyu1 [priv]
5651 5653 5651 5651 ? -1 S 500 0:00 \_ sshd: deyu1@pts/0
5653 5654 5654 5654 pts/0 6151 Ss 500 0:00 \_ -bash
5654 5881 5881 5654 pts/0 6151 S 0 0:00 \_ su
5881 5882 5882 5654 pts/0 6151 S 0 0:00 \_ bash
5882 6151 6151 5654 pts/0 6151 R+ 0 0:00 \_ ps -axjf
# 還可以使用 pstree 來達成這個程序樹。
#範例五:列出程序 crond
[root@dywHome2 ~]# ps -C crond
PID TTY TIME CMD
2018 ? 00:00:00 crond
ps aux
各個顯示項目代表的意義:
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 (疆屍) 程序的狀態。
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.
For BSD formats and when the stat keyword is used,
additional characters may be displayed:
< 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
START
:該 process 被觸發啟動的時間;
TIME
:該 process 實際使用 CPU 運作的時間。
COMMAND
:該程序的實際指令為何?
[root@dywOffice ~]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 5 0.0 0.0 0 0 ? SN Nov16 0:00 [ksoftirqd/1]
root 923 0.0 0.0 2104 488 ? S<s Nov16 0:00 udevd -d
dywang 29848 0.0 0.2 3460 2076 pts/1 Ss Dec20 0:00 /bin/bash
dywang 29724 0.0 2.1 47904 16392 tty1 S Dec20 0:12 gcin
[Ctrl]+[Esc]
)
[root@linux ~]# pstree [-Aup]
參數:
-A :各程序樹之間的連接以 ASCII 字元來連接;
-p :並同時列出每個 process 的 PID;
-u :並同時列出每個 process 的所屬帳號名稱。
[dywang@dywOffice ~]$ pstree -A
init-+-acpid
|-atd
|-avahi-daemon
|-clamd
|-crond
|-cupsd
|-6*[dbus-daemon]
|-dcopserver
|-events/0
|-events/1
|-freshclam
|-gam_server
|-gpg-agent
|-hpiod
|-2*[ifplugd]
|-kaccess
|-kalarmd
|-kded
|-kdeinit-+-artsd
| |-kdevelop---{kdevelop}
| |-kile---bash
| |-kio_file
| |-kio_pop3
| |-klauncher
| |-konqueror
| |-konsole-+-bash
| | |-bash---pstree
| | `-2*[su---bash]
| |-kwikdisk
| |-kwin
| |-net_applet
| |-stardict
| `-xsettings-kde
|-kdesktop
|-kdesud
|-khelper
|-khubd
|-kicker
|-kio_uiserver
|-kirqd
|-6*[kjournald]
|-klipper
|-klogd
|-kmix
|-knotify
|-kontact---4*[{kontact}]
|-korgac
|-kseriod
|-ksmserver
|-ksoftirqd/0
|-ksoftirqd/1
|-kswapd0
|-kthread-+-aio/0
| |-aio/1
| |-ata/0
| |-ata/1
| |-kacpid
| |-kblockd/0
| |-kblockd/1
| `-2*[pdflush]
|-login---bash---startx---xinit-+-X
| `-startkde-+-gcin
| `-kwrapper
|-mandi
|-migration/0
|-migration/1
|-5*[mingetty]
|-proftpd
|-python
|-s2u
|-ssh-agent
|-syslogd
|-udevd
|-xfs
`-xinetd
代號
名稱
內容
1
SIGHUP
代表『讓該 PID 重新讀取自己的設定檔』,類似重新啟動。
2
SIGINT
代表用鍵盤輸入的 [ctrl]+c 來中斷一個程序的進行。
9
SIGKILL
代表強制中斷一個程序的進行。
15
SIGTERM
以正常的結束程序來終止該程序。
如果該程序已經發生問題, 輸入這個 signal 也無法終止。
18
SIGCONT
繼續執行暫停的程序。
20
SIGTSTP
來自終端機的暫停,利用 [ctrl]+z 發出此訊號。
[Ctrl]+z
暫停執行,再以 ps 找出此程序之 PID 並以 kill 強制刪除。
[dywang@dywOffice ~]$ touch test
[dywang@dywOffice ~]$ vi test
[1]+ Stopped vi test
[dywang@dywOffice ~]$ ps -C vi
PID TTY TIME CMD
7770 pts/1 00:00:00 vi
[dywang@dywOffice ~]$ kill -15 7770
[dywang@dywOffice ~]$ ps -C vi
PID TTY TIME CMD
7770 pts/1 00:00:00 vi
[dywang@dywOffice ~]$ kill -9 7770
[dywang@dywOffice ~]$ ps -C vi
PID TTY TIME CMD
[1]+ Killed vi test
[root@linux ~]# killall [-iIe] [command name]
參數:
-i :interactive 互動式的,若需要刪除時,會出現提示字元;
-e :exact,表示『後面接的 command name 要一致』,但整個完整的指令不能超過 15 個字元。
-I :指令名稱(可能含參數)忽略大小寫。
[dywang@dywOffice ~]$ vi test
[1]+ Stopped vi test
[dywang@dywOffice ~]$ killall -9 vi
[1]+ Killed vi test
[dywang@dywOffice ~]$ jobs
練習題