กลับไปหน้าสูตร
#systemd#linux#service#cheatsheet

systemd Cheatsheet

รวมคำสั่ง systemd แบบจัดเต็ม: service lifecycle, unit files, journal logs, timers, startup behavior และ troubleshooting

11 มีนาคม 2569อ่านประมาณ 2 นาที

Service Lifecycle

Commandใช้ทำอะไร
sudo systemctl start <service>start service
sudo systemctl stop <service>stop service
sudo systemctl restart <service>restart service
sudo systemctl reload <service>reload config แบบไม่ restart
sudo systemctl status <service>ดูสถานะ
sudo systemctl is-active <service>เช็ก active/failed
sudo systemctl is-enabled <service>เช็ก enable at boot

Enable / Disable at Boot

Commandใช้ทำอะไร
sudo systemctl enable <service>ให้รันตอนบูต
sudo systemctl disable <service>ยกเลิก auto start
sudo systemctl reenable <service>disable+enable ใหม่
sudo systemctl mask <service>บล็อกไม่ให้ start ได้
sudo systemctl unmask <service>ยกเลิก mask

Journal Logs

Commandใช้ทำอะไร
journalctl -u <service>ดู logs service
journalctl -u <service> -ffollow logs
journalctl -u <service> --since "1 hour ago"logs ย้อนหลังช่วงเวลา
journalctl -xeu <service>logs error แบบละเอียด
journalctl --disk-usageดูพื้นที่ logs

Unit Files

Pathความหมาย
/etc/systemd/systemcustom units ของผู้ใช้ระบบ
/lib/systemd/system หรือ /usr/lib/systemd/systemunits จากแพ็กเกจ

Common Unit Commands

Commandใช้ทำอะไร
systemctl list-units --type=serviceดู services ที่โหลดอยู่
systemctl list-unit-files --type=serviceดู unit files ทั้งหมด
systemctl cat <service>แสดง unit file ที่ใช้งานจริง
systemctl show <service>แสดง properties
sudo systemctl daemon-reloadreload unit files หลังแก้ไฟล์

Example Service Unit

[Unit]
Description=My API Service
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/opt/my-api
ExecStart=/usr/bin/node server.js
Restart=always
RestartSec=3
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

Timers (แทน cron)

Commandใช้ทำอะไร
systemctl list-timersดู timers
systemctl status <name>.timerสถานะ timer
systemctl start <name>.timerstart timer
systemctl enable <name>.timerenable timer ตอนบูต

User Services

Commandใช้ทำอะไร
systemctl --user status <service>ดู user-level service
systemctl --user start <service>start user service
systemctl --user enable <service>enable user service

Troubleshooting Playbook

ปัญหาวิธีเช็ก
service ไม่ขึ้นsystemctl status + journalctl -xeu
start แล้วดับทันทีตรวจ ExecStart, permission, env vars
แก้ unit แล้วไม่เปลี่ยนลืม daemon-reload
เปิดเครื่องแล้วไม่รันเช็ก is-enabled และ [Install]
restart loopเช็ก Restart= และ root cause logs