ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
Power failure/restart ဖြစ်ရင် Python script (LED control, sensor monitor) ကို manual ပြန် run ရမယ်ဆိုရင် inconvenient ပါတယ် — systemd service အဖြစ် register လုပ်ထားရင် Linux tutorial ရဲ့ systemctl enable pattern အတိုင်း Pi boot တိုင်း script က automatic run ပါလိမ့်မယ်။ Service file (.service) ကို /etc/systemd/system/ ထဲ ဖန်တီးပြီး, ExecStart (run ရမည့် command), WorkingDirectory, User field များ configure ရပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
my-project.service ဖိုင် ဖန်တီးပြီး, [Service] section ထဲမှာ ExecStart=/usr/bin/python3 /home/pi/my-project/main.py ကို ရေး, sudo systemctl enable my-project.service (Linux tutorial ကနေ) ကို run ရင် boot တိုင်း auto-start ဖြစ်ပါလိမ့်မယ် — sudo systemctl status my-project.service ဖြင့် run နေ/မနေ စစ်ဆေးနိုင်ပါတယ် (Linux tutorial ရဲ့ systemctl status pattern)။
အတူတူ ကြည့်မယ်
# /etc/systemd/system/my-project.service
[Unit]
Description=My Raspberry Pi Project
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/my-project/main.py
WorkingDirectory=/home/pi/my-project
User=pi
Restart=always
[Install]
WantedBy=multi-user.target
# Then:
# sudo systemctl enable my-project.service
# sudo systemctl start my-project.servicesudo systemctl status my-project.service ကို run ရင် 'active (running)' ဖြင့် service run နေကြောင်း confirm ရမည်, Pi ကို restart လုပ်လည်း script auto-run ဆက်ဖြစ်နေမည်။၅ မိနစ် စမ်းကြည့်
Linux tutorial ရဲ့ systemd lesson ကို ပြန်ကြည့်ပြီး, ကိုယ့် Python script (ရှိရင်) အတွက် service file ကို ကိုယ်တိုင် ရေးကြည့်ပါ။
သတိလေးတစ်ချက်
Restart=always ကို script ထဲမှာ bug ရှိတဲ့အချိန် သုံးရင် crash-restart loop (continuously crash ပြီး restart) ဖြစ်နိုင်ပါတယ် — script ကို service အဖြစ် register မလုပ်ခင် manual run ဖြင့် stable ကြောင်း အရင် confirm ပါ။