systemctl 管服務狀態,journalctl 看 systemd journal。排錯時先 status、is-active、log,再決定 reload 或 restart;不要一看到錯誤就先重開。
動手前:restart 會中斷服務,enable 會改開機行為,daemon-reload 只重讀 unit 定義。正式環境先確認影響、相依服務與回復方式。
查詢服務
看服務完整狀態systemd Linux
systemctl status nginx.service包含 active 狀態、主程序與最近幾行 log。
只判斷是否 activesystemd Linux
systemctl is-active nginx.service適合腳本依結束碼判斷。
看是否設為開機啟動systemd Linux
systemctl is-enabled nginx.serviceenabled 不代表目前一定在執行。
列出執行中的服務systemd Linux
systemctl list-units --type=service --state=running只列目前已載入且執行中的服務。
列出失敗 unitsystemd Linux
systemctl --failed修復後可再 reset-failed。
看 unit 定義與 drop-insystemd Linux
systemctl cat nginx.service不必猜實際載入哪個檔案。
看服務相依關係systemd Linux
systemctl list-dependencies nginx.service可協助判斷重啟影響。
啟停與設定
啟動服務systemd Linux
sudo systemctl start nginx.service只改目前狀態,不自動設為開機啟動。
正常停止服務systemd Linux
sudo systemctl stop nginx.service先確認停機窗口與使用者影響。
重載服務設定systemd Linux
sudo systemctl reload nginx.service只有 unit 宣告並支援 reload 才有效。
重新啟動服務systemd Linux
sudo systemctl restart nginx.service可能中斷現有連線;先查 log 與設定語法。
設定開機啟動systemd Linux
sudo systemctl enable nginx.service不一定同時啟動目前服務。
取消開機啟動systemd Linux
sudo systemctl disable nginx.service不會自動停止目前正在跑的服務。
重讀 unit 檔systemd Linux
sudo systemctl daemon-reload修改 unit 或 drop-in 後使用,不會重啟服務。
清除 failed 狀態systemd Linux
sudo systemctl reset-failed nginx.service只清狀態,不修根因。
journalctl 查 log
看單一服務 logsystemd Linux
journalctl -u nginx.service預設可能從最舊開始並進 pager。
看最近 100 行systemd Linux
journalctl -u nginx.service -n 100 --no-pager適合工單或快速檢查。
即時追蹤服務 logsystemd Linux
journalctl -u nginx.service -f按 Ctrl+C 停止追蹤。
只看這次開機systemd Linux
journalctl -b排除舊開機的訊息。
看上一次開機systemd Linux
journalctl -b -1前提是 journal 有保存前次開機資料。
依時間篩選systemd Linux
journalctl --since '2026-08-11 09:00:00' --until '2026-08-11 10:00:00'用主機時區解讀;跨區事件要記錄時區。
只看 warning 以上systemd Linux
journalctl -p warning..alert -b優先級範圍依 journalctl 語法。
看 journal 磁碟用量systemd Linux
journalctl --disk-usage只查詢,不會清理。
怎麼確認有做對
- systemctl is-active 應回 active。
- status 不應持續出現 restart loop 或 failed。
- journalctl 的新 log 應對應本次操作與正常請求。
常見錯誤
- 把 daemon-reload 當成服務 restart。
- 只清 failed 狀態,沒有處理根因。
- restart 前沒跑應用程式自己的 config test。
- journal 沒持久化,卻期待看到上一次開機。
版本與官方文件
參數會隨工具版本與作業系統實作改變。正式環境先用 --help、-h 或系統內建說明確認,再以當版官方文件為準。