進程(process)維護,每一個被執行的程式都會有一個 pid (process id) 以被辨認,Linux 使用者必須學會如何掌握這些進程。例如顯示目前所有的進程以及如何停止程式等。

BashJobControl

當你使用 Bash 時,有些方法可以控制你的程序

將某一指令設定在背景中執行:

Command &
將執行中的程式使用 <Ctrl><z> 中斷後,使用 bg 來將這一個指令丟到背景中執行。
bg %JobID? 、 %JobID? & 將被暫停的程式移至背景執行。
 

檢示所有在背景中執行被 <Ctrl><z> 暫停的程式:jobs,

+ 表示是最後被暫停的,優先權最高。- 是表示再次級的。

將在背景中執行中或暫停中的程式移至前景執行:

fg[#]ProcessID?
fg%JobID?
%、%+、%%:將最後被暫停的程式移至前景執行。
%String:將被暫停的程式中,以String為開頭的程式移至前景執行。

procps

這裡介紹 DebianPackages:procps 的相關指令。DebianPackages:procps 可用來讀取 /proc 檔案系統,也就是讀取目前系統執行的程式狀態。計畫首頁 http://procps.sourceforge.net/

索引


ps

 

檢示目前正在執行中的 process:ps,可和 grep 指令搭配使用。

ps 參數前面不用加 『-』。
-a : 顯示在所有終端機下所執行的程序。
a : 顯示在現在的終端機下所執行的所有程序。
-A : 顯示所有的程序。包含所有的服務。
-f : 顯示 UID、PPIP、C、STIME 欄位。
-H : 顯示樹狀結構,表達程序間的相互關係。
S : 列出程序時,包含『已死亡』 的子程序資訊。
u : 以使用者程序為主的格示來顯示。
v : 以虛擬記憶體的格式來顯示。
V : 顯示版本資訊。
x : 顯示所有的程序,不以終端機來分別。

檢示正在背景中執行的程式:

user@debian:~$ ps -aux

kill

刪除在背景中執行的程式:

$ kill % JobID(使用 jobs 來取得 JobID)
$ /bin/kill[#]ProcessID(使用 ps 來取得 ProcessID)
$ /bin/kill -HUP/-1:重新啟動某個 process。

如果無法刪除,試試

/bin/kill 15[#]ProcessID

如果還是無法刪除,試試

/bin/kill 9[#]ProcessID。

nice

nice 設定 Precess 的優先順序:(可用 ps -l 中的 NI 欄位來檢視 Process 的優先順序。)-19 為最高而 20 為最低。

nice Command &(將程式放到背景執行,同時指定優先順序為 10。)
nice # Command &(將程式放到背景執行,同時指定優先順序為#。)

改變 Precess 的優先順序:

renice # [#]ProcessID(將指定的背景中執行程式的優先順序設定為 #。)
renice -u UserName # [#]ProcessID(將指定的使用者的背景中執行程式的優先順序全部設定為#。)

top 指令介紹

top 用於即時檢示目前的系統使用概況。會顯示所有執行中的程序(task)狀態,如ZombieProcess等資訊。預設以 CPU 使用率排序,可於介面按 <q> 離開。

按 <M> 會以 Memory 使用率排序。
按 <T> 會以已執行時間排序。
按 <C> 可以檢視命令列的參數。
按 <p> 可以檢視命令列的參數。
按 <u> 可以檢視特定的使用者。
按 <p> 可以直接刪除某一特定的 Process。
按 <s> 來指定更新的頻率。

vmstat

vmstat 可用來紀錄 processes, memory, paging, block IO, traps, 與 cpu activity。

例如

$ vmstat 1 # 1 是更新速度,單位為秒
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 1  0 549368  13316 102388  91992   12    6     9     8    2    12 72  3 25  0
 2  0 549368  13316 102388  91992    0    0     0     0  156  1516 98  2  0  0
 2  0 549368  13312 102388  91992    0    0     0     0  125   534 99  1  0  0

.... 

第一列與第二列所代表是執行程式的資訊,接下來的是記憶體、swap、io、systm 與 cpu。

   Procs
       r: The number of processes waiting for run time.
       b: The number of processes in uninterruptible sleep.
                                                                                
   Memory
       swpd: the amount of virtual memory used.
       free: the amount of idle memory.
       buff: the amount of memory used as buffers.
       cache: the amount of memory used as cache.
       inact: the amount of inactive memory. (-a option)
       active: the amount of active memory. (-a option)
                                                                                
   Swap
       si: Amount of memory swapped in from disk (/s).
       so: Amount of memory swapped to disk (/s).
 
   IO
       bi: Blocks received from a block device (blocks/s).
       bo: Blocks sent to a block device (blocks/s).
    System
       in: The number of interrupts per second, including the clock.
       cs: The number of context switches per second.
 
   CPU
       These are percentages of total CPU time.
       us: Time spent running non-kernel code. (user time, including nice time)
       sy: Time spent running kernel code. (system time)
       id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
       wa: Time spent waiting for IO. Prior to Linux 2.5.41, shown as zero.

watch

可以使用 watch 命令來反覆執行特定命令。如:watch -n 60 who;其中 -n 指更新的頻率。

ulimit

限制使用者最多可以執行的 Process 數量:ulimit -Hu 50

查自使用者最多可以執行的 Process 數量:ulimit -a

nohup

將某一指令設定在登出後繼續在背景中執行,例如將 cat 丟在背景執行

$ nohup cat &