Thuta Learning
ရှာဖွေရန်
ExercisesDevOps & Toolsbeginner

Exercise: Scripting, Process Monitoring နဲ့ Server Troubleshooting စိန်ခေါ်မှု

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Exercise: Scripting, Process Monitoring နဲ့ Server Troubleshooting စိန်ခေါ်မှု ကို ကိုယ်တိုင် လေ့ကျင့်ကြည့်မယ်
  • သင်ခဲ့ပြီးသား skill တွေကို practice လုပ်ပြီး ခိုင်မာအောင်လုပ်မယ်
  • အမှားရှာ၊ ပြင်တတ်၊ ကိုယ်တိုင် check လုပ်တတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီ lesson က tutorial ရဲ့ Intermediate/Advanced chapter တွေမှာ သင်ခဲ့တဲ့ shell script, process management, cron, disk, ssh/scp/rsync, systemd, log troubleshooting skill တွေကို တစ်ခုတည်း သီးခြားမဟုတ်ဘဲ ပေါင်းစပ်ပြီး သုံးစေတဲ့ scenario-based task တွေပါ။ Real server operator တစ်ယောက် နေ့စဉ်ကြုံရတဲ့ 'process မ run ဘူး', 'disk space ကုန်နေတယ်', 'remote server ကို file ပို့ရမယ်' စတဲ့ ပြဿနာမျိုးတွေကို simulate လုပ်ကြည့်တာပါ။ Task 1 ကနေ 4 ဆီကို difficulty တဆင့်ချင်း မြင့်တက်သွားပါတယ် — နောက်ဆုံး task က troubleshooting mindset ကိုပါ စမ်းစစ်ပါလိမ့်မယ်။ Command တစ်ခုချင်းစီ run ပြီးတိုင်း output ကို သေချာဖတ်ပြီးမှ နောက် step ဆက်ပါ။

လေ့ကျင့်ခန်းများ

Task 1: process တစ်ခု (ဥပမာ sleep 500 &) ကို background မှာ run ထားပြီး, ဒီ process name ကို ps aux | grep နဲ့ ရှာကာ run နေရင် echo "Running" မဟုတ်ရင် echo "Not running" ထုတ်ပေးတဲ့ check-process.sh script ကို ရေးပါ။ Task 2: အထက်က script ကို crontab -e နဲ့ 5 မိနစ်တစ်ခါ run အောင် schedule လုပ်ပြီး, result ကို ~/check.log ထဲ append (>>) ဖြစ်အောင် ချိတ်ပါ။ Task 3: /var/log (ဒါမှမဟုတ် permission ရတဲ့ log directory) အောက်က file တွေကို du -ah | sort -rh | head -5 နဲ့ size အကြီးဆုံး 5 ခု ရှာပြီး, 7 ရက်ထက်ကြီးတဲ့ log file တွေကို tar -czf ဖြင့် archive ပြီး ဖျက်ပါ (find ... -mtime +7 ကို tar/rm နဲ့ ပေါင်းသုံးပါ)။ Task 4: local test user account တစ်ခု (သို့) home VM/VPS တစ်ခုကို ssh-keygen + ssh-copy-id နဲ့ passwordless SSH setup လုပ်ပြီး, rsync -avz ကို သုံးကာ folder တစ်ခုလုံး remote ကို sync လုပ်ကြည့်ပါ — sync ပြီးရင် ssh remote-host "md5sum file" နဲ့ local checksum ကို ယှဉ်စစ်ပါ။

Code နမူနာ

bash
#!/bin/bash
# check-process.sh — Task 1 & 2
PROCESS_NAME="sleep"

if ps aux | grep -v grep | grep -q "$PROCESS_NAME"; then
  echo "$(date): $PROCESS_NAME is Running" >> ~/check.log
else
  echo "$(date): $PROCESS_NAME is NOT running" >> ~/check.log
fi

# crontab -e ထဲမှာ ဒီလို ထည့်ပါ (5 မိနစ်တစ်ခါ):
# */5 * * * * /bin/bash ~/check-process.sh

# Task 3: largest files + old log archive
du -ah /var/log 2>/dev/null | sort -rh | head -5
find /var/log -type f -mtime +7 -print0 | tar -czvf old-logs.tar.gz --null -T -
find /var/log -type f -mtime +7 -delete

# Task 4: passwordless SSH + rsync sync
ssh-keygen -t ed25519 -f ~/.ssh/practice_key -N ""
ssh-copy-id -i ~/.ssh/practice_key.pub user@remote-host
rsync -avz -e "ssh -i ~/.ssh/practice_key" ~/practice-lab/ user@remote-host:~/practice-lab-copy/
You should see
check.log ထဲမှာ timestamp တွဲပြီး process status log ဝင်နေမှာဖြစ်ပြီး, old-logs.tar.gz ထဲမှာ 7 ရက်ထက်ကြီးတဲ့ log ဖိုင်တွေ archive ဖြစ်နေကာ, remote-host ပေါ်မှာ practice-lab-copy folder ပြည့်စုံစွာ sync ဖြစ်နေပါလိမ့်မယ်။

၅ မိနစ် စမ်းကြည့်

5 မိနစ်အတွင်း check-process.sh ကို process name PROCESS_NAME="nonexistent123" လို့ ပြောင်းပြီး run ကြည့်ကာ, log file ထဲမှာ Not running status မှန်ကန်စွာ ရေးမိလားဆိုတာ tail -f ~/check.log နဲ့ live monitor လုပ်ကြည့်ပါ။

သတိလေးတစ်ချက်

find ... -delete နဲ့ tar archive command တွေကို production log directory ပေါ်မှာ တိုက်ရိုက်စမ်းမသုံးပါနဲ့ — test folder တစ်ခု ဖန်တီးပြီးမှ practice လုပ်ပါ, real /var/log ဆွဲပြင်ရင် system log history ပျောက်သွားနိုင်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • ps aux | grep process-name ရိုက်တဲ့အခါ grep command ကိုယ်တိုင်ကလည်း result ထဲ ပါလာနိုင်တာ (grep matching itself) — grep -v grep ထည့်ဖို့ မေ့တတ်တာ
  • crontab ထဲမှာ script path ကို absolute path မသုံးဘဲ ~/check-process.sh လို relative သုံးထားလို့ cron environment ထဲမှာ script ကို ရှာမတွေ့တဲ့ error

အခု ကိုယ်တိုင် စမ်းကြည့်

5 မိနစ်အတွင်း check-process.sh ကို process name PROCESS_NAME="nonexistent123" လို့ ပြောင်းပြီး run ကြည့်ကာ, log file ထဲမှာ Not running status မှန်ကန်စွာ ရေးမိလားဆိုတာ tail -f ~/check.log နဲ့ live monitor လုပ်ကြည့်ပါ။

You'll know it worked when: check.log ထဲမှာ timestamp တွဲပြီး process status log ဝင်နေမှာဖြစ်ပြီး, old-logs.tar.gz ထဲမှာ 7 ရက်ထက်ကြီးတဲ့ log ဖိုင်တွေ archive ဖြစ်နေကာ, remote-host ပေါ်မှာ practice-lab-copy folder ပြည့်စုံစွာ sync ဖြစ်နေပါလိမ့်မယ်။

Exercise: Scripting, Process Monitoring နဲ့ Server Troubleshooting စိန်ခေါ်မှု | Thuta Learning