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

Cron နဲ့ Task စီစဉ်ခြင်း

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

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

  • Cron နဲ့ Task စီစဉ်ခြင်း ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • Terminal ထဲမှာ ကိုယ်တိုင် command ကို စမ်းသုံးတတ်အောင် လုပ်မယ်
  • Real server/project မှာ ဒီ command ဘယ်လို အသုံးဝင်သလဲ ချိတ်မြင်မယ်

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

cron ဆိုတာ background မှာ အမြဲ run နေတဲ့ scheduler daemon (service) ပါ — crontab (cron table) ဖိုင်ထဲမှာ 'ဘယ်အချိန် ဘာ command run မလဲ' ဆိုတဲ့ rule တွေ ရေးထားရပါတယ်။ crontab -e ဖြင့် ကိုယ့် crontab ကို edit လုပ်ပါတယ်, crontab -l ဖြင့် ရှိပြီးသား rule များကို ကြည့်ပါတယ်။ Syntax ပုံစံက minute hour day month weekday command ဖြစ်ပြီး field တစ်ခုစီမှာ * ထားရင် 'အချိန်တိုင်း' ဆိုတဲ့ အဓိပ္ပာယ်ပါ — ဥပမာ 0 2 * * * /path/to/backup.sh ဆိုရင် 'နေ့တိုင်း မနက် ၂ နာရီတိုင်း backup.sh ကို run ပါ' ဆိုတာပါ (0 = minute 0, 2 = hour 2, * * * = နေ့တိုင်း/လတိုင်း/weekday အားလုံး)။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Server backup, log rotation, database cleanup လိုမျိုး 'ပုံမှန် ထပ်ခါထပ်ခါ လုပ်ရမယ့်' task တွေအတွက် cron က perfect ပါ — manual reminder ထားပြီး ကိုယ်တိုင် run နေစရာ မလိုတော့ပါဘူး။ crontab -e ကို ပထမဆုံးအကြိမ် run ရင် text editor ရွေးခိုင်းပါလိမ့်မယ် (nano ကို beginner-friendly လို့ အကြံပြုပါတယ်), ရွေးပြီးရင် rule line တစ်ကြောင်း ထည့်ပြီး save/close လုပ်ရုံပါ။ Cron job တွေက background မှာ run တာမို့ output ကို ကိုယ်တိုင် မမြင်ရပါဘူး — log ဖိုင်ထဲသို့ redirect (> သို့မဟုတ် >>) လုပ်ထားရင် ပြီးနောက် ပြန်စစ်ဆေးလို့ရပါတယ်။

အတူတူ terminal ထဲ စမ်းမယ်

text
# crontab -e ထဲမှာ ရေးရမယ့် rule format
# min hour day month weekday  command
0    2    *   *     *        /home/user/backup.sh
*/15 *    *   *     *        /home/user/check-health.sh
0    0    *   *     0        /home/user/weekly-report.sh >> /home/user/cron.log
You should see
crontab -l ကို run ရင် ကိုယ်ထည့်ထားတဲ့ schedule rule များကို ပြန်ပြသည်။

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

crontab -e (သို့) crontab -l ကို run ကြည့်ပါ (empty ဖြစ်နိုင်)။ 'နေ့တိုင်း မွန်းတည့် ၁၂ နာရီ backup.sh run ပါ' ဆိုတဲ့ rule line ကို ကိုယ်တိုင် ရေးကြည့်ပါ (run စရာမလို, syntax ကိုပဲ ကျင့်)။

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

cron job ထဲက script ကို run ခင် terminal မှာ ကိုယ်တိုင် test run လုပ်ပြီးမှ crontab ထဲ ထည့်ပါ — cron environment က terminal environment နဲ့ variable အချို့ ကွာနိုင်ပါတယ်။

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

  • cron rule ရဲ့ field ငါးခု (minute hour day month weekday) ကို order မှားရေးမိခြင်း
  • cron job command ကို relative path (./backup.sh) နဲ့ ရေးပြီး cron run ချိန် folder မှားနေလို့ error ဖြစ်ခြင်း — absolute path (/home/user/backup.sh) သုံးသင့်

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

crontab -e (သို့) crontab -l ကို run ကြည့်ပါ (empty ဖြစ်နိုင်)။ 'နေ့တိုင်း မွန်းတည့် ၁၂ နာရီ backup.sh run ပါ' ဆိုတဲ့ rule line ကို ကိုယ်တိုင် ရေးကြည့်ပါ (run စရာမလို, syntax ကိုပဲ ကျင့်)။

You'll know it worked when: crontab -l ကို run ရင် ကိုယ်ထည့်ထားတဲ့ schedule rule များကို ပြန်ပြသည်။

Cron နဲ့ Task စီစဉ်ခြင်း | Thuta Learning