Home »
IT Knowledge »
สอนใช้ systemctl จัดการ Service บน Linux สำหรับ System Admin 2026
สอนใช้ systemctl จัดการ Service บน Linux สำหรับ System Admin 2026
systemd คืออะไร?
systemd คือ Init System และ Service Manager หลักของ Linux Distribution ส่วนใหญ่ในปัจจุบัน (Ubuntu, Debian, CentOS, RHEL, Fedora, Arch) ทำหน้าที่ เริ่มต้นระบบ, จัดการ Service, Mount Filesystem, จัดการ Network และอื่น ๆ อีกมากมาย
systemd vs Init vs Upstart
| คุณสมบัติ |
SysV Init |
Upstart |
systemd |
| ปีที่เริ่มใช้ |
1983 |
2006 |
2010 |
| Boot Speed |
ช้า (Sequential) |
เร็วขึ้น (Event-based) |
เร็วที่สุด (Parallel) |
| Service Config |
Shell Scripts (/etc/init.d/) |
.conf files (/etc/init/) |
.service files (/etc/systemd/) |
| Dependency |
Manual ordering |
Event-based |
Declarative (After=, Requires=) |
| Process Tracking |
PID files |
ptrace |
cgroups (แม่นยำที่สุด) |
| Logging |
syslog |
syslog |
journald (binary, structured) |
| Socket Activation |
ไม่มี |
ไม่มี |
มี (เหมือน launchd ของ macOS) |
| Timer |
cron แยก |
cron แยก |
Timer Units (ทดแทน cron ได้) |
| สถานะปัจจุบัน |
Legacy |
Deprecated |
มาตรฐาน 2026 |
systemctl — คำสั่งหลักในการจัดการ Service
คำสั่งพื้นฐาน
# Start/Stop/Restart Service:
sudo systemctl start nginx # เริ่ม Service
sudo systemctl stop nginx # หยุด Service
sudo systemctl restart nginx # Restart (Stop แล้ว Start ใหม่)
sudo systemctl reload nginx # Reload Config (ไม่หยุด Service)
sudo systemctl reload-or-restart nginx # Reload ถ้าได้ ไม่งั้น Restart
# Enable/Disable (Auto-start on Boot):
sudo systemctl enable nginx # เปิดเครื่องมา Start อัตโนมัติ
sudo systemctl disable nginx # ปิด Auto-start
sudo systemctl enable --now nginx # Enable + Start ทันที
# Check Status:
systemctl status nginx # ดูสถานะ (Active, PID, Memory, Logs)
systemctl is-active nginx # ตอบ active หรือ inactive
systemctl is-enabled nginx # ตอบ enabled หรือ disabled
systemctl is-failed nginx # ตอบ failed หรือ active
ตัวอย่าง Output ของ systemctl status
# $ systemctl status nginx
# ● nginx.service - A high performance web server and a reverse proxy server
# Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
# Active: active (running) since Wed 2026-04-16 10:30:00 +07; 2h ago
# Docs: man:nginx(8)
# Main PID: 1234 (nginx)
# Tasks: 5 (limit: 4915)
# Memory: 12.4M
# CPU: 1.234s
# CGroup: /system.slice/nginx.service
# ├─1234 "nginx: master process /usr/sbin/nginx"
# ├─1235 "nginx: worker process"
# └─1236 "nginx: worker process"
#
# สิ่งที่ต้องดู:
# - Loaded: ไฟล์ .service อยู่ที่ไหน + enabled/disabled
# - Active: active (running) = ทำงานปกติ
# - Main PID: PID ของ Process หลัก
# - Memory/CPU: Resource ที่ใช้
# - CGroup: Process Tree
Listing Services
# แสดง Service ทั้งหมดที่ทำงานอยู่:
systemctl list-units --type=service
# แสดง Service ทั้งหมด (รวมที่ไม่ Active):
systemctl list-units --type=service --all
# แสดงเฉพาะ Service ที่ Failed:
systemctl list-units --type=service --state=failed
# แสดง Unit Files ทั้งหมด:
systemctl list-unit-files --type=service
# กรอง Service:
systemctl list-units --type=service | grep mysql
systemctl list-unit-files --type=service --state=enabled
# แสดง Timer ทั้งหมด:
systemctl list-timers
systemctl list-timers --all
journalctl — ดู Log ของ Service
# ดู Log ของ Service เฉพาะ:
journalctl -u nginx # ดู Log ทั้งหมดของ nginx
journalctl -u nginx -f # Follow Log แบบ Real-time (เหมือน tail -f)
journalctl -u nginx --since today # Log วันนี้
journalctl -u nginx --since "2026-04-16 10:00:00" # Log ตั้งแต่เวลาที่กำหนด
journalctl -u nginx --since "1 hour ago" # Log 1 ชั่วโมงที่ผ่านมา
journalctl -u nginx --until "2026-04-16 12:00:00" # Log จนถึงเวลาที่กำหนด
journalctl -u nginx --since "2026-04-15" --until "2026-04-16"
# ดู Log ทั้งระบบ:
journalctl # Log ทั้งหมด
journalctl -b # Log ตั้งแต่ Boot ล่าสุด
journalctl -b -1 # Log จาก Boot ก่อนหน้า
journalctl --list-boots # แสดง Boot ทั้งหมด
# ดู Log ตาม Priority:
journalctl -p err # เฉพาะ Error ขึ้นไป
journalctl -p warning # เฉพาะ Warning ขึ้นไป
# Priority: emerg, alert, crit, err, warning, notice, info, debug
# ดู Kernel Log:
journalctl -k # เหมือน dmesg แต่ persistent
# จำกัดจำนวนบรรทัด:
journalctl -u nginx -n 50 # แสดง 50 บรรทัดล่าสุด
journalctl -u nginx --no-pager # ไม่ใช้ Pager (แสดงทั้งหมด)
# Output Format:
journalctl -u nginx -o json # JSON format
journalctl -u nginx -o json-pretty # JSON แบบอ่านง่าย
journalctl -u nginx -o short-iso # ISO timestamp
สร้าง Custom systemd Service
# ตัวอย่าง: สร้าง Service สำหรับ Python App
# 1. สร้างไฟล์ Service:
sudo nano /etc/systemd/system/myapp.service
# 2. เนื้อหาไฟล์:
[Unit]
Description=My Python Web Application
After=network.target postgresql.service
Wants=postgresql.service
Documentation=https://docs.myapp.com
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/opt/myapp
Environment=PYTHONPATH=/opt/myapp
Environment=DATABASE_URL=postgresql://localhost/myapp
ExecStartPre=/opt/myapp/venv/bin/python manage.py migrate --check
ExecStart=/opt/myapp/venv/bin/gunicorn --workers 4 --bind 0.0.0.0:8000 myapp.wsgi
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=myapp
# Resource Limits:
LimitNOFILE=65535
MemoryMax=512M
CPUQuota=200%
[Install]
WantedBy=multi-user.target
# 3. โหลด Unit File ใหม่:
sudo systemctl daemon-reload
# 4. Enable + Start:
sudo systemctl enable --now myapp
# 5. ตรวจสอบ:
systemctl status myapp
journalctl -u myapp -f
อธิบาย Sections
| Section |
Directive |
ความหมาย |
| [Unit] |
Description |
คำอธิบาย Service |
|
After |
เริ่มหลัง Unit นี้ |
|
Wants |
ต้องการ Unit นี้ (Soft Dependency) |
|
Requires |
ต้องมี Unit นี้ (Hard Dependency) |
| [Service] |
Type |
simple, forking, oneshot, notify, idle |
|
ExecStart |
คำสั่งเริ่ม Service |
|
ExecReload |
คำสั่ง Reload |
|
Restart |
on-failure, always, on-abnormal |
|
RestartSec |
รอกี่วินาทีก่อน Restart |
|
User/Group |
Run as User/Group |
| [Install] |
WantedBy |
Target ที่จะ Enable ใน |
Timer Units — ทดแทน Cron
# Timer Units เป็น Replacement สำหรับ cron ที่ทำงานร่วมกับ systemd ได้ดีกว่า
#
# ข้อดีเหนือ cron:
# - มี Log ใน journald (cron ต้องดู syslog)
# - จัดการ Dependencies ได้
# - Persistent (ถ้าพลาด Schedule จะรันทันทีที่ได้)
# - Resource Control (Limit CPU, Memory)
# ตัวอย่าง: สร้าง Timer สำหรับ Backup ทุกวัน 02:00
# 1. สร้าง Service File:
# /etc/systemd/system/backup.service
[Unit]
Description=Daily Backup
[Service]
Type=oneshot
ExecStart=/opt/scripts/backup.sh
StandardOutput=journal
StandardError=journal
# 2. สร้าง Timer File:
# /etc/systemd/system/backup.timer
[Unit]
Description=Run Backup Daily at 2 AM
[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
RandomizedDelaySec=300
[Install]
WantedBy=timers.target
# 3. Enable Timer:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
# 4. ตรวจสอบ:
systemctl list-timers
systemctl status backup.timer
journalctl -u backup.service
Timer Schedule Syntax
# OnCalendar Syntax:
# *-*-* 02:00:00 = ทุกวัน 02:00
# Mon *-*-* 03:00:00 = ทุกวันจันทร์ 03:00
# *-*-01 00:00:00 = วันที่ 1 ของทุกเดือน 00:00
# *-01-01 00:00:00 = 1 มกราคมทุกปี
# Mon,Fri *-*-* 06:00:00 = จันทร์และศุกร์ 06:00
# *-*-* *:00:00 = ทุกชั่วโมง (ต้นชั่วโมง)
# *-*-* *:*:00 = ทุกนาที
# *-*-* *:00/15:00 = ทุก 15 นาที (00, 15, 30, 45)
#
# ทดสอบ Schedule:
systemd-analyze calendar "*-*-* 02:00:00"
systemd-analyze calendar "Mon *-*-* 03:00:00"
# → แสดงว่า Next trigger เมื่อไร
# OnBootSec / OnUnitActiveSec (Monotonic Timers):
# OnBootSec=5min = 5 นาทีหลัง Boot
# OnUnitActiveSec=1h = ทุก 1 ชั่วโมงหลัง Service ทำงานล่าสุด
Socket Activation
# Socket Activation คือการเริ่ม Service เมื่อมี Connection เข้ามา
# ไม่ต้องรัน Service ตลอดเวลา → ประหยัด Resource
#
# ตัวอย่าง: HTTP Service ที่เริ่มเมื่อมี Request
# /etc/systemd/system/myhttp.socket
[Unit]
Description=My HTTP Socket
[Socket]
ListenStream=0.0.0.0:8080
Accept=false
[Install]
WantedBy=sockets.target
# /etc/systemd/system/myhttp.service
[Unit]
Description=My HTTP Service
Requires=myhttp.socket
[Service]
Type=simple
ExecStart=/opt/myhttp/server
StandardInput=socket
# เริ่ม Socket (ไม่ใช่ Service):
sudo systemctl enable --now myhttp.socket
# → Service จะเริ่มเมื่อมี Connection เข้า Port 8080
Service Dependencies
# Dependency Directives:
#
# After=B.service → A เริ่มหลัง B (Ordering only)
# Before=A.service → B เริ่มก่อน A (Ordering only)
# Requires=B.service → ถ้า B ตาย A ก็ตาย (Hard Dependency)
# Wants=B.service → ถ้า B ตาย A ยังทำงาน (Soft Dependency)
# BindsTo=B.service → เหมือน Requires แต่ถ้า B Stop A ก็ Stop
#
# ตัวอย่าง: Web App ที่ต้องการ Database
[Unit]
Description=Web Application
After=network.target postgresql.service redis.service
Requires=postgresql.service
Wants=redis.service
# → เริ่มหลัง network, postgresql, redis
# → ถ้า postgresql ตาย → Web App ตายด้วย
# → ถ้า redis ตาย → Web App ยังทำงาน (graceful degradation)
# ดู Dependencies:
systemctl list-dependencies nginx
systemctl list-dependencies nginx --reverse # ใครขึ้นกับ nginx
Service Hardening (Sandboxing)
# systemd มี Security Options สำหรับ Sandbox Service:
[Service]
# Filesystem:
ProtectSystem=strict # Mount /usr, /boot, /efi เป็น Read-Only
ProtectHome=true # ซ่อน /home, /root, /run/user
ReadWritePaths=/var/lib/myapp # อนุญาต Write เฉพาะ Path นี้
PrivateTmp=true # /tmp แยกจาก Service อื่น
# Network:
PrivateNetwork=false # ใช้ Network ได้ (true = ปิด Network)
RestrictAddressFamilies=AF_INET AF_INET6 # จำกัด Network Family
# Capabilities:
CapabilityBoundingSet=CAP_NET_BIND_SERVICE # จำกัด Linux Capabilities
NoNewPrivileges=true # ห้ามได้ Privilege เพิ่ม
# System Calls:
SystemCallFilter=@system-service # อนุญาตเฉพาะ System Calls ที่จำเป็น
SystemCallArchitectures=native # ป้องกัน 32-bit Exploit
# ตรวจสอบ Security Score:
systemd-analyze security myapp.service
# → แสดง Security Score (0-10, ต่ำ = ปลอดภัย)
Troubleshooting Failed Services
# Service Failed? ทำอย่างไร:
#
# 1. ดู Status:
systemctl status myapp.service
# → ดู Active: failed, Exit Code
# 2. ดู Log:
journalctl -u myapp.service -n 50 --no-pager
# → ดู Error Message
# 3. ดู Configuration:
systemctl cat myapp.service
# → ดูไฟล์ .service จริง ๆ
# 4. ตรวจ Syntax:
systemd-analyze verify myapp.service
# → ตรวจว่า Unit File ถูกต้อง
# 5. ดู Dependencies ที่พัง:
systemctl list-dependencies myapp.service
# → ดูว่า Dependency ไหน Failed
# 6. Reset Failed State:
sudo systemctl reset-failed myapp.service
# → Reset สถานะ Failed แล้วลอง Start ใหม่
# Common Problems:
# - ExecStart path ผิด
# - Permission denied (User/Group ไม่ถูก)
# - Port ถูกใช้อยู่แล้ว (Address already in use)
# - Dependency Service ยังไม่ Start
# - Environment variable ไม่ถูกต้อง
# - Working Directory ไม่มี
คำสั่ง systemctl ที่ใช้บ่อย (Quick Reference)
| คำสั่ง |
ความหมาย |
systemctl start/stop/restart SERVICE |
เริ่ม/หยุด/Restart Service |
systemctl enable/disable SERVICE |
เปิด/ปิด Auto-start |
systemctl status SERVICE |
ดูสถานะ |
systemctl reload SERVICE |
Reload Config |
systemctl daemon-reload |
โหลด Unit Files ใหม่ |
systemctl list-units --type=service |
แสดง Service ทั้งหมด |
systemctl list-timers |
แสดง Timer ทั้งหมด |
systemctl cat SERVICE |
แสดงไฟล์ Service |
systemctl edit SERVICE |
แก้ไข Override |
systemctl mask/unmask SERVICE |
ป้องกัน/ปลดป้องกัน Start |
journalctl -u SERVICE -f |
Follow Log |
journalctl -u SERVICE --since today |
Log วันนี้ |
systemd-analyze blame |
ดูเวลา Boot ของแต่ละ Service |
systemd-analyze security SERVICE |
Security Score |
สรุป: systemctl เป็น Must-Know สำหรับ System Admin
systemd + systemctl คือเครื่องมือพื้นฐานที่ System Admin ทุกคนต้องรู้ในปี 2026 ไม่ว่าจะจัดการ Web Server, Database, Application Server หรือ Background Job ทุกอย่างล้วนเป็น systemd Service ความสามารถในการสร้าง Custom Service, ตั้ง Timer, อ่าน Journal Log, และ Troubleshoot Failed Services จะช่วยให้คุณจัดการ Linux Server ได้อย่างมืออาชีพ