Lab 3 — Linux Process & Systemd
1. Mục tiêu
Ở Lab 1, chúng ta học cách làm việc với file và log. Ở Lab 2, chúng ta học user, group và permission.
Trong lab này, chúng ta sẽ tìm hiểu process và service — hai khái niệm xuất hiện liên tục khi quản trị Linux server.
Sau lab này, bạn có thể:
- Hiểu process là gì.
- Tìm process đang chạy.
- Kiểm tra CPU và RAM của process.
- Dừng process bằng
kill. - Hiểu
systemdvàsystemctl. - Tạo một Linux service.
- Xem log của service bằng
journalctl. - Debug một application bị crash hoặc không khởi động.
2. Process là gì?
Khi bạn chạy:
./app
Linux sẽ tạo một process để thực thi application đó.
Có thể hình dung:
Application
↓
Process
↓
PID
Ví dụ:
sleep 1000
Mở terminal khác:
ps aux | grep sleep
Bạn có thể thấy:
devops 1234 0.0 0.0 ... sleep 1000
1234 chính là PID — Process ID.
Mỗi process đang chạy trên Linux đều có một PID.
3. Xem các Process
Command cơ bản:
ps
Xem process của tất cả user:
ps aux
Ví dụ:
USER PID %CPU %MEM COMMAND
root 1 0.0 0.2 /sbin/init
devops 1234 1.2 0.5 java -jar app.jar
Một command rất hay dùng:
ps aux | grep nginx
Có thể hiểu:
ps aux
↓
Liệt kê process
↓
grep nginx
↓
Tìm process chứa "nginx"
Tip: Khi troubleshooting,
ps aux | grep <application>thường là một trong những command đầu tiên nên thử.
4. Theo dõi Process Realtime
ps chỉ cho bạn một snapshot tại thời điểm chạy command.
Nếu muốn theo dõi realtime:
top
Hoặc nếu đã cài:
htop
Bạn sẽ thấy:
PID
USER
CPU
MEM
TIME
COMMAND
Ví dụ:
PID CPU% MEM% COMMAND
1234 85.2 2.1 java -jar app.jar
5678 0.5 0.3 nginx
Điều này giúp trả lời nhanh:
Process nào đang tiêu thụ CPU hoặc RAM?
5. PID và PPID
Mỗi process có:
PID → ID của chính nó
PPID → ID của process cha
Ví dụ:
systemd
│
└── application
│
└── child process
Kiểm tra:
ps -ef
Hoặc:
ps -o pid,ppid,cmd
Ví dụ:
PID PPID CMD
1000 1 java -jar app.jar
Process có PPID = 1 thường đã được gắn vào process init/systemd.
Điều này sẽ rất hữu ích khi chúng ta tìm hiểu systemd.
6. Process State
Process không phải lúc nào cũng chạy liên tục.
Một số trạng thái thường gặp:
R → Running
S → Sleeping
D → Uninterruptible sleep
Z → Zombie
T → Stopped
Xem state:
ps -eo pid,ppid,state,cmd
Ví dụ:
PID PPID S CMD
1234 1 S java -jar app.jar
Không cần nhớ tất cả ngay từ đầu.
Điều quan trọng là biết:
Khi application có vấn đề, hãy kiểm tra process có thực sự đang chạy hay không.
7. Kill Process
Giả sử process có:
PID = 1234
Có thể gửi signal:
kill 1234
Mặc định là:
SIGTERM
Đây là cách yêu cầu process tự dừng một cách graceful.
Nếu process không chịu dừng:
kill -9 1234
-9 là:
SIGKILL
Process sẽ bị kill ngay lập tức.
⚠️ Không nên lạm dụng kill -9
Ưu tiên:
kill PID
chờ application tự cleanup.
Chỉ dùng:
kill -9 PID
khi process thực sự không phản hồi.
Kinh nghiệm thực tế:
SIGTERMcho application cơ hội đóng connection, flush dữ liệu và cleanup.SIGKILLthì không.
Điều này đặc biệt quan trọng khi bạn làm Kubernetes, vì Kubernetes cũng sử dụng cơ chế graceful termination trước khi force kill container.
8. Thực hành Process
Tạo một process:
sleep 300
Terminal khác:
ps aux | grep sleep
Tìm PID.
Ví dụ:
devops 1234 ... sleep 300
Dừng process:
kill 1234
Kiểm tra:
ps aux | grep sleep
Process đã biến mất.
9. Process tiêu thụ CPU
Tạo process CPU cao:
yes > /dev/null
Mở terminal khác:
top
Bạn sẽ thấy CPU tăng cao.
Tìm process:
ps aux | grep yes
Sau đó:
kill <PID>
Kiểm tra lại:
top
CPU sẽ giảm.
Đây là một scenario troubleshooting thực tế:
Server CPU = 100%
↓
top
↓
Tìm process
↓
Xác định PID
↓
ps -fp <PID>
↓
Tìm nguyên nhân
10. Systemd là gì?
Trong server thực tế, chúng ta không muốn phải chạy:
java -jar app.jar
bằng tay mỗi lần server khởi động.
Ta muốn:
Server boot
↓
systemd
↓
Todo API
↓
Application running
systemd là hệ thống quản lý service phổ biến trên Linux.
Một service có thể:
- Start application.
- Stop application.
- Restart application.
- Tự khởi động khi server reboot.
- Quản lý dependency.
- Ghi log.
11. systemctl
Các command quan trọng:
systemctl status
systemctl start
systemctl stop
systemctl restart
systemctl enable
systemctl disable
Ví dụ:
systemctl status ssh
Start:
sudo systemctl start nginx
Stop:
sudo systemctl stop nginx
Restart:
sudo systemctl restart nginx
Kiểm tra:
sudo systemctl status nginx
12. start và enable khác nhau
Chạy nginx ngay bây giờ.
sudo systemctl start nginx
Cho nginx tự động khởi động khi server boot.
sudo systemctl enable nginx
Muốn vừa chạy ngay vừa tự động start:
Tip:
enable --nowrất tiện khi setup server.
sudo systemctl enable --now nginx
13. Tạo Application Service
Bây giờ chúng ta tạo một application đơn giản.
Tạo script:
sudo mkdir -p /opt/todo-app
sudo nano /opt/todo-app/app.sh
Nội dung:
#!/bin/bash
while true
do
echo "Todo API is running"
sleep 10
done
Cấp quyền:
sudo chmod +x /opt/todo-app/app.sh
Test:
/opt/todo-app/app.sh
Dừng bằng:
Ctrl + C
14. Tạo systemd Service
Tạo:
sudo nano /etc/systemd/system/todo-app.service
Nội dung:
[Unit]
Description=Todo Application
After=network.target
[Service]
ExecStart=/opt/todo-app/app.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
Sau khi tạo file:
sudo systemctl daemon-reload
Start:
sudo systemctl start todo-app
Kiểm tra:
sudo systemctl status todo-app
Nếu thành công:
Active: active (running)
15. Enable Service
Cho service tự chạy khi server reboot:
sudo systemctl enable todo-app
Hoặc:
sudo systemctl enable --now todo-app
Kiểm tra:
systemctl is-enabled todo-app
Kết quả:
enabled
16. Xem Process của Service
Bây giờ:
ps aux | grep app.sh
Bạn sẽ thấy process:
root ... /bin/bash /opt/todo-app/app.sh
Có thể hình dung:
systemd
↓
todo-app.service
↓
app.sh
↓
Process
Đây chính là mối quan hệ quan trọng cần hiểu.
17. journalctl — Xem Log Service
Một trong những command quan trọng nhất:
journalctl
Xem log của Todo App:
journalctl -u todo-app
Xem log realtime:
journalctl -u todo-app -f
Xem log từ lần boot hiện tại:
journalctl -u todo-app -b
Xem những dòng gần nhất:
journalctl -u todo-app -n 50
Kinh nghiệm thực tế: Khi một service không start được,
systemctl statuscho bạn triệu chứng, cònjournalctlthường cho bạn nguyên nhân.
18. Thực hành Debug Service
Bây giờ hãy cố tình tạo lỗi.
Sửa:
sudo nano /etc/systemd/system/todo-app.service
Đổi:
ExecStart=/opt/todo-app/app.sh
thành:
ExecStart=/opt/todo-app/not-found.sh
Reload:
sudo systemctl daemon-reload
Restart:
sudo systemctl restart todo-app
Kiểm tra:
sudo systemctl status todo-app
Bạn sẽ thấy service failed.
19. Debug đúng cách
Đừng đoán.
Hãy đi theo thứ tự:
Bước 1 — Status
systemctl status todo-app
Bước 2 — Log
journalctl -u todo-app -n 50
Bước 3 — Kiểm tra file
ls -l /opt/todo-app/
Bước 4 — Kiểm tra command
/opt/todo-app/app.sh
Bước 5 — Kiểm tra permission
ls -l /opt/todo-app/app.sh
Sau khi sửa:
sudo systemctl daemon-reload
sudo systemctl restart todo-app
20. Restart Policy
Trong service:
Restart=on-failure
nghĩa là:
Application crash
↓
systemd phát hiện
↓
Restart application
Một số giá trị thường gặp:
no
on-success
on-failure
always
Trong application production, restart policy rất hữu ích.
Ví dụ:
Todo API
↓
Crash
↓
systemd
↓
Restart
Tuy nhiên, restart liên tục không đồng nghĩa application đã được chữa lỗi.
Nếu application crash 100 lần thì systemd chỉ đang che triệu chứng.
21. Bài tập tổng hợp
Hãy tạo service:
todo-worker.service
chạy:
/opt/todo-app/worker.sh
Worker phải:
1. Chạy liên tục
2. Ghi log mỗi 5 giây
3. Tự restart khi crash
4. Tự start khi server reboot
Service cần có:
Restart=on-failure
và:
sudo systemctl enable todo-worker
Sau đó kiểm tra:
systemctl status todo-worker
journalctl -u todo-worker -f
22. Challenge — Application bị crash
Tạo script:
sudo nano /opt/todo-app/crash.sh
Nội dung:
#!/bin/bash
echo "Application started"
sleep 10
echo "Application crashed"
exit 1
Cấp quyền:
sudo chmod +x /opt/todo-app/crash.sh
Tạo service sử dụng script này.
Đặt:
Restart=on-failure
Sau đó quan sát:
systemctl status crash-app
và:
journalctl -u crash-app -f
Bạn sẽ thấy:
Application started
↓
Application crashed
↓
Process exit 1
↓
systemd restart
↓
Application started
↓
...
23. Tư duy Troubleshooting
Khi một application trên Linux không chạy, hãy kiểm tra theo flow:
Application không hoạt động
↓
Service có chạy?
↓
systemctl status
↓
Có process?
↓
ps / pgrep
↓
Có crash?
↓
journalctl
↓
Kiểm tra config
↓
Kiểm tra permission
↓
Kiểm tra port/network
Đừng bắt đầu bằng:
kill -9
hoặc:
chmod 777
hay restart server.
Hãy tìm nguyên nhân trước.
24. Một số command cần nhớ
| Command | Mục đích |
|---|---|
ps aux |
Xem process |
ps -ef |
Xem process + PPID |
top |
Theo dõi resource realtime |
htop |
Process monitor dễ nhìn hơn |
pgrep |
Tìm PID |
kill |
Gửi signal cho process |
kill -9 |
Force kill |
systemctl status |
Kiểm tra service |
systemctl start |
Start service |
systemctl stop |
Stop service |
systemctl restart |
Restart service |
systemctl enable |
Auto-start khi boot |
journalctl |
Xem system/service logs |
25. Kinh nghiệm thực tế cần nhớ
1. systemctl status chưa đủ
Khi service fail:
systemctl status app
sau đó gần như luôn nên xem:
journalctl -u app
2. restart không phải troubleshooting
Sai:
Application lỗi
↓
restart
↓
lỗi tiếp
↓
restart
Đúng:
Application lỗi
↓
status
↓
logs
↓
process
↓
config
↓
permission
↓
network
↓
fix
3. Hiểu SIGTERM trước SIGKILL
Ưu tiên:
kill PID
thay vì:
kill -9 PID
Graceful shutdown rất quan trọng với database và application server.
4. Process ≠ Service
Đây là điểm cần nhớ:
Process
→ một chương trình đang chạy
Service
→ cách hệ thống quản lý application/process
Ví dụ:
systemd
↓
todo-app.service
↓
Java Process
↓
Spring Boot
26. Liên hệ với Kubernetes
Những kiến thức trong lab này sẽ xuất hiện lại trong Kubernetes dưới một hình thức khác:
Linux
──────────────────
systemd
↓
process
↓
application
Kubernetes:
Kubernetes
──────────────────
Deployment
↓
Pod
↓
Container
↓
Application Process
Tương tự như:
systemd restart
Kubernetes cũng có cơ chế:
Container crash
↓
Kubernetes detects
↓
Restart container
Và:
journalctl
sẽ dần được thay thế bằng cách xem:
kubectl logs
Do đó, hiểu Linux process và service trước sẽ giúp bạn hiểu bản chất Kubernetes thay vì chỉ học thuộc command.
27. Checklist
Sau Lab 3, bạn nên tự trả lời được:
✓ Process là gì?
✓ PID là gì?
✓ PPID là gì?
✓ Làm sao tìm process?
✓ Làm sao biết process đang dùng nhiều CPU?
✓ SIGTERM khác SIGKILL thế nào?
✓ systemd là gì?
✓ systemctl dùng để làm gì?
✓ start khác enable thế nào?
✓ journalctl dùng để làm gì?
✓ Restart=on-failure có ý nghĩa gì?
✓ Làm sao debug service failed?
✓ Process và Service khác nhau thế nào?
Nếu bạn có thể tự tạo một todo-app.service, làm cho nó tự start khi reboot, tự restart khi crash, và tìm được nguyên nhân khi service fail bằng systemctl + journalctl, thì bạn đã nắm được phần cốt lõi của Lab 3.
Lab tiếp theo: Lab 4 — Linux Resource & Disk Management, nơi chúng ta sẽ xử lý những tình huống rất thực tế như CPU 100%, RAM gần đầy, disk 100%, log chiếm hết ổ đĩa và tìm process gây ra vấn đề.
All rights reserved