เมื่อ Server ทำงานช้าหรือ CPU/RAM พุ่งสูงผิดปกติ คำสั่งแรกที่ System Administrator ใช้ตรวจสอบคือ top เพื่อดูว่า Process ใดกินทรัพยากรมากที่สุด Linux มีเครื่องมือ System Monitoring หลายตัวที่ตอบโจทย์ต่างกัน ตั้งแต่ top ที่มีติดตั้งมาทุก Distro, htop ที่ใช้งานง่ายกว่าด้วย UI แบบ Interactive ไปจนถึง glances ที่แสดงภาพรวมระบบแบบครบวงจร
บทความนี้อธิบายการใช้งาน top, htop และ glances สำหรับ Real-time System Monitoring ตั้งแต่การอ่านค่า CPU/RAM, การเรียงลำดับ Process, การ Kill Process จากภายใน Monitor รวมถึงการใช้งาน glances ในโหมด Web และ API
top — System Monitor มาตรฐาน
top เป็นเครื่องมือที่ติดมากับทุก Linux Distribution แสดงข้อมูล CPU, Memory และ Process แบบ Real-time อัพเดตทุก 3 วินาทีโดยค่าเริ่มต้น
# เปิด top
top
# เปิด top สำหรับ User เฉพาะคน
top -u www-data
# เปิด top แบบ Batch (ไม่ Interactive — เหมาะสำหรับ Script)
top -b -n 1
# แสดง Process ทั้งหมด 20 อันดับแรก
top -b -n 1 | head -30
อ่านค่าในหน้า top
# ส่วนหัว — Summary ของระบบ
top - 10:30:00 up 5 days, 2:15, 2 users, load average: 0.52, 0.48, 0.45
Tasks: 180 total, 1 running, 179 sleeping, 0 stopped, 0 zombie
%Cpu(s): 5.2 us, 1.1 sy, 0.0 ni, 93.2 id, 0.3 wa, 0.0 hi, 0.2 si
MiB Mem : 3938.0 total, 412.3 free, 2105.4 used, 1420.3 buff/cache
MiB Swap: 2048.0 total, 1988.2 free, 59.8 used. 1544.6 avail Mem
# คำอธิบาย
# load average: 0.52, 0.48, 0.45 — ค่าเฉลี่ย 1, 5, 15 นาที (ถ้าสูงกว่าจำนวน CPU core = overload)
# us = user CPU, sy = system CPU, id = idle, wa = wait I/O
# buff/cache = หน่วยความจำที่ใช้เป็น Cache (คืนได้เมื่อจำเป็น)
# avail Mem = หน่วยความจำที่ใช้ได้จริงรวม Cache
คำสั่ง Keyboard ใน top
# การเรียงลำดับ
P — เรียงตาม CPU Usage (ค่าเริ่มต้น)
M — เรียงตาม Memory Usage
T — เรียงตาม Time (เวลา CPU สะสม)
N — เรียงตาม PID
# การแสดงผล
1 — แสดง CPU แต่ละ Core แยกกัน
H — แสดง Threads แทน Processes
c — แสดง Full Command (แทนชื่อ Process)
V — แสดงแบบ Tree (Parent-Child)
u — กรอง Process ตาม User
# การ Kill Process
k — Kill Process (พิมพ์ PID แล้วกด Enter)
จากนั้นเลือก Signal (15 = SIGTERM, 9 = SIGKILL)
# อื่น ๆ
d — เปลี่ยน Refresh Interval (วินาที)
q — ออกจาก top
htop — Interactive Process Viewer
htop เป็นทางเลือกที่ใช้งานง่ายกว่า top มาก รองรับ Mouse, มี Color-coded Display, และ Kill Process ได้โดยไม่ต้องจำ PID
# ติดตั้ง htop
sudo apt install htop # Ubuntu/Debian
sudo dnf install htop # RHEL/Rocky
# เปิด htop
htop
# เปิดพร้อม Filter User
htop -u deploy
# เปิดพร้อมระบุ Delay (วินาที)
htop -d 5 # อัพเดตทุก 5 วินาที
คำสั่ง Keyboard ใน htop
# Navigation
↑↓ — เลื่อนเลือก Process
F5 — แสดงแบบ Tree View
F4 — กรอง (Filter) ตามชื่อ Process
# การเรียงลำดับ
F6 — เลือก Column สำหรับเรียง
กด Header — คลิก Column Header เพื่อเรียง
# การจัดการ Process
F9 — Kill Process (เลือก Signal จากเมนู)
F7/F8 — ปรับ Nice value (Priority)
Space — Tag Process (เพื่อ Kill หลาย Process พร้อมกัน)
# มุมมอง
F2 — Settings (ปรับหน้าจอ)
F1 — Help
# ออก
q / F10 — ออกจาก htop
Meters แถบบนของ htop
# แถบ CPU — แสดงสัดส่วนการใช้งาน
# สีเขียว (user), สีน้ำเงิน (system/kernel), สีแดง (IRQ), สีส้ม (softIRQ)
# แถบ Memory
# สีเขียว = used, สีน้ำเงิน = buffer, สีเหลือง = cache
# Load Average — 3 ตัวเลข (1m, 5m, 15m)
# ค่ามาตรฐาน: ไม่ควรเกินจำนวน CPU core
# Uptime — เวลาที่ระบบทำงานต่อเนื่อง
glances — System Overview แบบครบวงจร
glances เป็น Cross-platform Monitoring Tool ที่แสดงข้อมูลครบกว่า top/htop ในหน้าจอเดียว ทั้ง CPU, Memory, Disk I/O, Network, Docker Containers และ Sensor อุณหภูมิ รองรับทั้งโหมด Terminal, Web Browser และ REST API
# ติดตั้ง glances
pip install glances --break-system-packages # ติดตั้งด้วย pip
sudo apt install glances # Ubuntu/Debian (อาจเป็นเวอร์ชันเก่า)
sudo dnf install glances # RHEL/Rocky
# เปิด glances (Terminal mode)
glances
ข้อมูลที่ glances แสดง
# CPU — ใช้งาน, Idle, System, User, IOWait
# Load Average — 1m, 5m, 15m
# Memory/Swap — Total, Used, Free, Cache
# Network — รับ/ส่งข้อมูลต่อวินาที แต่ละ Interface
# Disk I/O — Read/Write IOPS แต่ละ Disk
# File System — พื้นที่ใช้งานแต่ละ Mount Point
# Processes — Process ที่ใช้ทรัพยากรมากสุด
# Docker — Container Status (ถ้าติดตั้ง Docker)
คำสั่ง Keyboard ใน glances
q / Esc — ออกจาก glances
1 — แสดง CPU แต่ละ Core
c — เรียงตาม CPU
m — เรียงตาม Memory
i — เรียงตาม Disk I/O
n — แสดง/ซ่อน Network
d — แสดง/ซ่อน Disk I/O
f — แสดง/ซ่อน File System
t — แสดง Network แบบ Combined
glances โหมด Web และ API
จุดเด่นของ glances คือรองรับการ Monitor ผ่าน Web Browser และ REST API ทำให้ Monitor Server จากระยะไกลได้โดยไม่ต้อง SSH
# เปิด glances Web Server (port 61208)
glances -w
# ระบุ Port และ Bind Address
glances -w --port 61208 --bind 0.0.0.0
# เปิดพร้อม Password
glances -w --password
# เข้าถึงผ่าน Browser
# http://server-ip:61208/
# REST API — ดึงข้อมูล CPU
curl http://localhost:61208/api/3/cpu
curl http://localhost:61208/api/3/mem
curl http://localhost:61208/api/3/load
curl http://localhost:61208/api/3/processlist
รัน glances เป็น Service (systemd)
# สร้าง systemd unit file
sudo nano /etc/systemd/system/glances.service
# เนื้อหาของ unit file
[Unit]
Description=Glances System Monitor
After=network.target
[Service]
ExecStart=/usr/bin/glances -w --port 61208 --bind 0.0.0.0 -t 5
Restart=on-failure
[Install]
WantedBy=multi-user.target
# เปิดใช้งาน
sudo systemctl daemon-reload
sudo systemctl enable --now glances
sudo systemctl status glances
เปรียบเทียบ top, htop และ glances
# top
# + ติดมากับทุก Linux ไม่ต้องติดตั้งเพิ่ม
# + ใช้ทรัพยากรน้อยที่สุด
# - UI ดูยาก ต้องจำ Keyboard Shortcut
# htop
# + UI สวยงาม มีสีและ Progress Bar
# + รองรับ Mouse
# + Kill Process ได้สะดวก ไม่ต้องจำ PID
# - ต้องติดตั้งเพิ่ม (apt/dnf install htop)
# glances
# + แสดงข้อมูลครบที่สุด (CPU/RAM/Disk/Network/Docker)
# + มีโหมด Web UI และ REST API
# + เหมาะกับ Remote Monitoring
# - ใช้ทรัพยากรมากกว่าสองตัวแรก
# - ต้องติดตั้ง Python/pip
เทคนิคเพิ่มเติมสำหรับ top/htop
# บันทึก top output ลงไฟล์ (Batch mode)
top -b -n 3 -d 5 > /tmp/top-output.txt
# -b = batch, -n 3 = 3 รอบ, -d 5 = interval 5 วินาที
# ดู Process เฉพาะตัวด้วย PID
top -p 1234
top -p 1234,5678,9012 # หลาย PID
# ดู CPU Usage เฉพาะ Process ด้วย htop + strace
htop -p 1234
# ตรวจสอบ Process ที่ใช้ CPU สูงจาก Command Line
ps aux --sort=-%cpu | head -10
ps aux --sort=-%mem | head -10
สรุป
Linux มีเครื่องมือ System Monitoring สามระดับที่เหมาะกับการใช้งานต่างกัน top เหมาะสำหรับการตรวจสอบเบื้องต้นเพราะติดมาทุก Distro, htop เหมาะสำหรับการใช้งานประจำเพราะ UI อ่านง่ายและจัดการ Process ได้สะดวก, และ glances เหมาะสำหรับ Overview ทั้งระบบรวมถึง Remote Monitoring ผ่าน Web Browser การเข้าใจค่า Load Average, CPU States (us/sy/wa/id) และ Memory (used/cache/available) เป็นทักษะสำคัญในการ Troubleshoot ปัญหาประสิทธิภาพบน Production Server
แนะนำบริการ DE
การ Monitor ระบบแบบ Real-time ด้วย top, htop หรือ glances ต้องการสิทธิ์เข้าถึง Server เพื่อดู Process และทรัพยากรระบบ Cloud VPS ของ DE ให้ Root Access เต็มรูปแบบ สามารถติดตั้ง htop, glances และเปิด Web Monitoring ได้อย่างอิสระ เหมาะสำหรับ System Administrator ที่ต้องการ Monitor Production Server อย่างละเอียด
หากต้องการโฮสต์เว็บไซต์โดยไม่ต้องจัดการ Server Monitoring เอง Cloud Hosting ของ DE มีระบบจัดการทรัพยากรอัตโนมัติและแสดงสถิติการใช้งานผ่าน Control Panel

