Quản lý service Linux bằng systemd trong môi trường server production

Quản lý Linux service với systemd theo chuẩn production

Trong vận hành Linux production, nhiều lỗi downtime không đến từ ứng dụng quá phức tạp mà đến từ việc service restart sai cách, cấu hình unit file thiếu kiểm soát, hoặc không có quy trình quan sát trạng thái dịch vụ. Nếu anh quản lý VPS, bare-metal hoặc VM chạy nhiều service, nắm chắc systemd là kỹ năng bắt buộc.

Vì sao nên chuẩn hóa vận hành với systemd?

  • Kiểm soát vòng đời service nhất quán
  • Theo dõi trạng thái và lỗi nhanh qua systemctl + journalctl
  • Tự động restart khi lỗi theo policy rõ ràng
  • Dễ audit, dễ transfer vận hành

Unit file mẫu

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

[Service]
Type=simple
User=www-data
WorkingDirectory=/opt/myapp
ExecStart=/usr/bin/node /opt/myapp/server.js
Restart=on-failure
RestartSec=5
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target

Lệnh quản lý cốt lõi

sudo systemctl daemon-reload
sudo systemctl start myapp
sudo systemctl status myapp --no-pager
sudo systemctl enable myapp
journalctl -u myapp -n 200 --no-pager

Troubleshooting nhanh

  • Exited 203/EXEC: sai đường dẫn ExecStart
  • Service restart vòng lặp: dependency chưa sẵn sàng
  • Không auto-start: quên enable
  • Permission denied: sai user/quyền thư mục

Kết luận

Systemd là nền tảng vận hành Linux production ổn định, dễ debug và tự phục hồi tốt hơn khi có sự cố.

Leave a Comment

Your email address will not be published. Required fields are marked *