サービス管理
systemd、ログ管理
現代のLinuxディストリビューションではsystemdがinit systemとして 使用されています。サービスの起動、停止、依存関係管理、ログ収集を統合的に行います。
基本コマンド
1 # サービス操作 2 systemctl start nginx 3 systemctl stop nginx 4 systemctl restart nginx 5 systemctl reload nginx # 設定再読み込み 6 7 # 状態確認 8 systemctl status nginx 9 systemctl is-active nginx 10 systemctl is-enabled nginx 11 12 # 自動起動設定 13 systemctl enable nginx 14 systemctl disable nginx 15 16 # サービス一覧 17 systemctl list-units --type=service 18 systemctl list-unit-files --type=service
ユニットタイプ
.serviceサービス・デーモン
.socketソケットアクティベーション
.timerタイマー(cron代替)
.targetユニットのグループ
.mountマウントポイント
.pathパス監視
ログ確認
1 # systemdジャーナル 2 journalctl 3 4 # 特定サービスのログ 5 journalctl -u nginx 6 7 # リアルタイム表示 8 journalctl -f 9 journalctl -u nginx -f 10 11 # 今日のログ 12 journalctl --since today 13 14 # ブート関連 15 journalctl -b # 現在のブート 16 journalctl -b -1 # 前回のブート
SRE/インフラ観点
運用で重要なポイント
- • サービス起動失敗時は
journalctl -u service -xeで詳細確認 - • 依存関係確認:
systemctl list-dependencies - • 設定変更後:
systemctl daemon-reloadを忘れずに - • 自動再起動設定:
Restart=always