Linux Processes, System Resources နှင့် Systemd Services
Server Resources စစ်ဆေးခြင်း (free, df, htop)
Section titled “Server Resources စစ်ဆေးခြင်း (free, df, htop)”Server လေးလံနေသည့်အခါ အဓိက အကြောင်းရင်း ၃ ခုဖြစ်သည့် CPU, RAM သို့မဟုတ် Disk Space ပြည့်မပြည့်ကို အရင်ဆုံး စစ်ဆေးရပါမည်:
# 1. RAM Memory သုံးစွဲမှုကို MB/GB ဖြင့် ရှင်းလင်းစွာ ကြည့်ခြင်းfree -h
# 2. Hard Disk Storage ပမာဏ ကျန်ရှိမှုကို ကြည့်ခြင်း (Disk Free)df -h
# 3. CPU Core အလိုက် နှင့် RAM သုံးစွဲနေသော Processes များကို Real-time UI ဖြင့် ကြည့်ခြင်းhtopRunning Processes များကို ရှာဖွေခြင်းနှင့် ပိတ်ပစ်ခြင်း (ps, kill)
Section titled “Running Processes များကို ရှာဖွေခြင်းနှင့် ပိတ်ပစ်ခြင်း (ps, kill)”အကယ်၍ Application တစ်ခုသည် Hang ဖြစ်နေပါက သို့မဟုတ် Port တစ်ခုကို မလွှတ်ဘဲ ဖြစ်နေပါက:
# Node.js နှင့် သက်ဆိုင်သော Running Process များကို ရှာမည်ps aux | grep node
# Port 3000 ကို မည်သည့် Process က သုံးနေကြောင်း ရှာမည်sudo lsof -i :3000ရှာတွေ့သော PID (Process ID) ကို သုံး၍ ပိတ်ပစ်နိုင်ပါတယ်:
# Process ID 14205 ကို အတင်းအကျပ် ပိတ်ပစ်မည် (-9 Force Kill)kill -9 14205
# node နာမည်ပါသော process အားလုံးကို တစ်ခါတည်း သတ်ပစ်မည်pkill -f nodeအဘယ်ကြောင့် node server.js ဟု တိုက်ရိုက် Run ရုံဖြင့် မလုံလောက်သနည်း?
Section titled “အဘယ်ကြောင့် node server.js ဟု တိုက်ရိုက် Run ရုံဖြင့် မလုံလောက်သနည်း?”Developer များ အလေ့အကျင့်မှားတစ်ခုမှာ Terminal တွင် node server.js & ဟု Run ထားခဲ့ခြင်း ဖြစ်သည်။ ၎င်းတွင် ဆိုးကျိုး ၃ ခု ရှိပါတယ်:
- SSH Terminal Session ပြတ်တောက်သွားပါက App ရပ်တန့်သွားနိုင်သည်။
- Unhandled Exception ကြောင့် App Crash ဖြစ်သွားပါက အလိုအလျောက် ပြန်မတက်နိုင်ပါ။
- Server Reboot ကျသွားပါက Application ပြန်လည် အလုပ်မလုပ်တော့ပါ။
ဖြေရှင်းချက်: Linux ၏ တရားဝင် Service Manager ဖြစ်သော Systemd ကို သုံးရပါမည်။
Production Systemd Service ဖန်တီးနည်း (လက်တွေ့ ဥပမာ)
Section titled “Production Systemd Service ဖန်တီးနည်း (လက်တွေ့ ဥပမာ)”Node.js Backend API အတွက် Systemd Service ဖိုင်တစ်ခု ရေးဆွဲကြည့်ပါမည်:
အဆင့် ၁: Service File အသစ် ဆောက်ခြင်း
Section titled “အဆင့် ၁: Service File အသစ် ဆောက်ခြင်း”sudo nano /etc/systemd/system/myapi.serviceအဆင့် ၂: Configuration ထည့်သွင်းခြင်း
Section titled “အဆင့် ၂: Configuration ထည့်သွင်းခြင်း”[Unit]Description=My Production Node.js API ServiceAfter=network.target
[Service]Type=simpleUser=ubuntuWorkingDirectory=/home/ubuntu/projects/my-apiExecStart=/usr/bin/node server.jsRestart=alwaysRestartSec=5Environment=NODE_ENV=production PORT=3000
[Install]WantedBy=multi-user.targetအဓိက အရေးကြီးသော အချက်များ:
Section titled “အဓိက အရေးကြီးသော အချက်များ:”User=ubuntu: လုံခြုံရေးအရ root အစား သာမန် user အနေဖြင့် Run စေခြင်း။Restart=always: Error ကြောင့် Crash ဖြစ်သွားပါက ၅ စက္ကန့်အတွင်း အလိုအလျောက် ပြန် Restart လုပ်ပေးရန် သတ်မှတ်ခြင်း။Environment: Production Environment Variables များကို သတ်မှတ်ပေးခြင်း။
systemctl ဖြင့် Service ကို ထိန်းချုပ်ခြင်း
Section titled “systemctl ဖြင့် Service ကို ထိန်းချုပ်ခြင်း”Service file ရေးဆွဲပြီးပါက Linux စနစ်ကို ပြန်လည် Reload လုပ်ပြီး စတင်ရပါမည်:
# 1. Systemd Daemon ကို Configuration အသစ် ဖတ်ခိုင်းခြင်းsudo systemctl daemon-reload
# 2. Service ကို ချက်ချင်း စတင် Run မည်sudo systemctl start myapi
# 3. Server Reboot ကျတိုင်း အလိုအလျောက် စတင် Run ရန် Enable လုပ်မည်sudo systemctl enable myapi
# 4. Service အခြေအနေ (Active / Running) မှန်မမှန် စစ်ဆေးမည်sudo systemctl status myapiအခြား အသုံးဝင်သော Commands များ:
sudo systemctl stop myapi # ရပ်တန့်မည်sudo systemctl restart myapi # ပြန်လည်စတင်မည်journalctl ဖြင့် Service Logs များကို Live စောင့်ကြည့်ခြင်း
Section titled “journalctl ဖြင့် Service Logs များကို Live စောင့်ကြည့်ခြင်း”Systemd ဖြင့် Run ထားသော Service ၏ console.log() အားလုံးကို journalctl ဖြင့် စစ်ဆေးနိုင်ပါတယ်:
# myapi ၏ နောက်ဆုံး Log များကို Real-time Streaming ဖြင့် စောင့်ကြည့်မည် (-f Follow, -u Unit)journalctl -u myapi -f
# နောက်ဆုံးထွက် Log လိုင်း ၁၀၀ ကိုသာ ကြည့်မည်journalctl -u myapi -n 100 --no-pager🧠 အသိပညာ စစ်ဆေးမှု (Quiz)
Section titled “🧠 အသိပညာ စစ်ဆေးမှု (Quiz)”Systemd & Process Management စစ်ဆေးမှု
မေးခွန်းများကို ဖြေဆိုပြီး မိမိ၏ နားလည်မှုကို စစ်ဆေးပါ
1. Systemd Service File တွင် 'Restart=always' ဟု ထည့်သွင်းခြင်း၏ အကျိုးကျေးဇူးမှာ အဘယ်နည်း?
ဒီသင်ခန်းစာ ဖတ်ပြီးပြီလား?
ပြီးမြောက်ကြောင်း မှတ်သားထားရန် အောက်ပါ ခလုတ်ကို နှိပ်ပါ