Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux ps 命令實戰精講:10 個高頻用法與進程監控技巧

編輯:Linux 教學

ps 語法風格

  • UNIX 風格:選項前加 -,可組合 (如 -ef)
  • BSD 風格:選項無 - (如 aux)
  • GNU 長選項:--sort、--forest 等
  • 可混用,但注意衝突

1. 無參數:當前終端進程

ps
  PID TTY          TIME CMD
 1234 pts/0    00:00:00 bash
 5678 pts/0    00:00:00 ps

2. 列出所有進程 (-a -x)

ps -ax | less

3. 按用戶過濾 (-u)

ps -u pungki

4. CPU/內存排序 (aux + --sort)

ps aux --sort=-%cpu | head -n 10   # CPU 佔用前 10
ps aux --sort=-%mem | head -n 10   # 內存佔用前 10
ps aux --sort=-%cpu,-%mem | head -n 10  # 綜合排序

5. 按進程名/PID 過濾 (-C)

ps -C nginx
ps -fC nginx          # 全格式顯示
ps -C nginx -o pid,ppid,cmd,%cpu,%mem  # 自定義欄位

6. 查看線程 (-L)

ps -L -p 1234   # 顯示 PID 1234 的所有線程 (LWP 欄)

7. 樹形顯示進程關係

ps -axjf
# 或
pstree -p

8. 安全審計:誰在登錄

ps -eo pid,user,args
# 顯示 PID、用戶、完整命令行

9. Root 進程審計

ps -U root -u root u
# -U 真實用戶ID,-u 有效用戶ID,u 用戶導向格式

10. 實時監控 (watch + ps)

watch -n 1 'ps aux --sort=-%mem,-%cpu | head -20'
watch -n 1 'ps aux -U pungki u --sort=-%mem,-%cpu | head -20'

優於 top/htop:可自定義顯示欄位、過濾用戶、導出 CSV 等。

常用欄位含義速查

欄位含義
PID進程 ID
PPID父進程 ID
%CPUCPU 佔用百分比
%MEM內存佔用百分比
VSZ虛擬內存大小 (KB)
RSS駐留集大小 (KB,實際物理內存)
TTY控制終端
STAT進程狀態 (R/S/D/Z/T 等)
START啟動時間
TIME累計 CPU 時間
COMMAND啟動命令

總結

掌握 aux --sort、-C、-L、-o 自定義輸出、watch 實時化,即可覆蓋 95% 進程排查場景。ps 普遍預裝,無需安裝,是運維必備核心技能。

Copyright © Windows教程網 All Rights Reserved