用於進程管理和作業控制的主要命令如下圖所示。
大部分命令都是對進程運行狀態進行查詢和監控的,而工作中與進程相關的也基本就是監控進程運行狀態,檢查影響系統運行瓶頸在哪裏,然後進行調憂工作。因此本文只着重介紹常用的進程監控命令的監控信息所代表的含義,至於命令怎麼使用可以參照man手冊或其他文檔。如:[12個進程管理命令介紹]
1.ps - report a snapshot of the current processes.
ps有三個不同風格的用法,GNU的長選項風格就不提了,比較難記。還是說說比較常用的兩個經典組合吧。一個是BSD風格的ps aux,一個是unix風格的ps -efF。
USER:啓動此進程的用戶
PID:進程號
%CPU:CPU佔用率
%MEM:內存佔用率
VSZ:虛擬內存集
RSS:常駐內存集
vsz VSZ virtual memory size of the process in KiB (1024-byte units). Device mappings are currently excluded; this is subject to change. (alias vsize). rss RSS resident set size, the non-swapped physical memory that a task has used (in kiloBytes). (alias rssize, rsz).
TTY:啓動此進程的終端,'?'表示系統後臺啓動的進程。
STAT:進程運行狀態
PROCESS STATE CODES Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process. 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:進程啓動時間/日期
TIME:在CPU上執行的累計時長
COMMAND:啓動此進程的命令,'[]'表示內核啓動的線程。
PPID:父進程號
C:CPU佔用率
SZ:進程實際佔用物理內存大小,包含文本、數據和棧空間。
PSR: 進程當前運行在CPU的哪個核心上
C pcpu cpu utilization sz SZ size in physical pages of the core image of the process. This includes text, data, and stack space. Device mappings are currently excluded; this is subject to change. See vsz and rss. psr PSR processor that process is currently assigned to.
2. top - display Linux processes
比較難懂點的就是load average和%CPU那兩行了。我覺得man手冊寫的挺好,直接貼了。
system load avg over the last 1, 5 and 15 minutes Line 2 shows CPU state percentages based on the interval since the last refresh. As a default, percentages for these individual categories are displayed. Where two labels are shown below, those for more recent kernel versions are shown first. us, user : time running un-niced user processes sy, system : time running kernel processes ni, nice : time running niced user processes id, idle : time spent in the kernel idle handler wa, IO-wait : time waiting for I/O completion hi : time spent servicing hardware interrupts si : time spent servicing software interrupts st : time stolen from this vm by the hypervisor
下面的字段含義:
VIRT:虛擬內存集
RES:常駐內存集
SHR:共享內存集
S: 進程運行狀態
20. S -- Process Status The status of the task which can be one of: D = uninterruptible sleep R = running S = sleeping T = stopped by job control signal t = stopped by debugger during trace Z = zombie
重點說下PR和NI
PR:進程優先級。1-99:實時優先級,數字越大優先級越高,系統自動安排;100-139:靜態優先級,數字越小優先級越高,可手動更改。說明:rt爲real time,實時優先級;20實際爲100+20,顯示爲20而已.
NI:Nice Value。-20至19,對應於優先級100到139。因此可以通過renice命令來調整nice值進而改變進程優先級,或者使用nice命令指定進程啓動的nice值,不指定時使用默認nice值0。普通用戶只能通過調整nice值降低進程優先級,而root則可以隨意調整。
3. htop - interactive process viewer
htop是由epel源提供的軟件包,需要額外安裝。界面美觀很多,甚至可以使用鼠標交互。字段信息跟top是一致的,不另外介紹了,你值得擁有。
4. dstat - versatile tool for generating system resource statistics
可以自定義參數的,默認使用了-cdngy。
-c, --cpu enable cpu stats (system, user, idle, wait, hardware interrupt, software interrupt -d, --disk enable disk stats (read, write) -n, --net enable network stats (receive, send) -g, --page enable page stats (page in, page out) -y, --sys enable system stats (interrupts, context switches)
選項含義和字段含義都解釋的很清楚吧,所以最好不過man手冊。
5. kill/killall/pkill
kill實際上是給進程發送信號量的,並不一定是殺死進程,也可以是讓進程reload或者從掛起狀態轉爲繼續運行狀態的。
以上是所有可用的信號量,而常用的一般是以下幾個:
1)SIGHUP:無須關閉進程而讓其重讀配置文件。 2)SIGINT:終止正在運行的進程,相當於Ctrl+C,中斷。 9)SIGKILL:殺死運行中的進程。 15)SIGTERM:終止運行中的進程;通常終止爲後臺進程。默認信號量 18)SIGCONT:繼續,讓後臺停止狀態進程在後臺繼續運行。 19)SIGSTOP:停止,將進程送後臺,停止狀態。
使用時,可以給信號量編號,也可以給信號量全稱或者是簡稱,不指定信號量,使用默認信號量。
killall和pkill也是同樣的原理。
6. 作業控制
系統作業分爲前臺作業和後臺作業。
前臺作業(foregroud):通過終端啓動,且啓動後會一直佔據終端。
後臺作業(backgroud):可以通過終端啓動,但啓動後即轉入後臺運行(釋放終端)。
讓作業運行於後臺
對於運行中作業使用Ctrl+Z組合鍵,將作業送往後臺,轉爲停止狀態。相當於發送了SIGSTOP信號量。
對於尚未啓動的作業使用在命令後加上'&'來啓動,像這樣。
[root@localhost ~]# COMMAND &
但這樣啓動的作業仍然與終端相關,如果希望將其與終端剝離,可以這樣。
[root@localhost ~]# nohup COMMAND &
使用jobs命令可以有哪些作業在後臺運行或者被掛起。
[root@localhost ~]# jobs [1]- Stopped top [2] Running nohup dstat & [3]+ Stopped ping 172.18.0.1
控制後臺作業
[root@localhost ~]# fg [[%]JOB_NUM]:把指定的作業調回前臺; [root@localhost ~]# bg [[%]JOB_NUM]:讓送往後臺的作業在後臺繼續運行; [root@localhost ~]# kill %JOB_NUM:終止指定的作業;
fg和bg的%可以省,kill的%是不能省的,省了就成了發送信號量了。fg和bg省略JOB_NUM時,默認是指帶+號的那個作業。