Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

伺服器自動重啟最佳實踐:時間選擇、頻率與實作腳本

編輯:關於伺服器

建議重啟時段與頻率

項目建議值理由
最佳時段凌晨 03:00 ~ 04:00(如 03:33)業務流量最低、無排程任務衝突、避開 Windows Update 預設安裝時段
建議頻率每月 1 次釋放記憶體碎片、清理暫存檔、重置網路連線池、套用核心更新需重啟
避開時段Windows Update 自動安裝時段(預設 03:00)若啟用自動更新,請錯開 30 分鐘以上,避免更新未完成即被強制重啟

實作方式:工作排程器 + shutdown.exe

圖形介面設定步驟

  1. 開啟 控制台 → 系統與安全性 → 系統管理工具 → 工作排程器。
  2. 右側點擊 建立基本工作。
  3. 名稱:Monthly Server Reboot,描述:每月凌晨自動重啟釋放資源。
  4. 觸發條件:每月 → 選擇具體日期(如 1 號)→ 時間 03:33。
  5. 動作:啟動程式。
    • 程式/腳本:C:\Windows\System32\shutdown.exe
    • 加入引數:-r -f -t 60 -c "Scheduled monthly reboot for maintenance"
      • -r:重啟
      • -f:強制關閉應用程式
      • -t 60:60 秒倒數(給使用者儲存機會)
      • -c:註解訊息
  6. 完成精靈 → 右鍵工作 → 內容 → 條件/設定 微調(如僅在網路可用時執行)。

命令列快速建立(管理員 PowerShell)

$action = New-ScheduledTaskAction -Execute "C:\Windows\System32\shutdown.exe" -Argument "-r -f -t 60 -c 'Monthly maintenance reboot'"
$trigger = New-ScheduledTaskTrigger -Monthly -DaysOfMonth 1 -At 3:33am
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd
Register-ScheduledTask -TaskName "MonthlyServerReboot" -Action $action -Trigger $trigger -Settings $settings -RunLevel Highest -Force

進階:批次檔包裝(可加入日誌、服務優雅停止)

@echo off
set LOG=C:\Logs\server_reboot_%date:~,4%%date:~5,2%%date:~8,2%.log
echo [%date% %time%] Initiating scheduled reboot... >> %LOG%
net stop "World Wide Web Publishing Service" >> %LOG% 2>&1
net stop "SQL Server (MSSQLSERVER)" >> %LOG% 2>&1
timeout /t 30
shutdown -r -f -t 30 -c "Scheduled maintenance reboot"
echo [%date% %time%] Reboot command sent. >> %LOG%

將上述存為 C:\Scripts\reboot_monthly.bat,排程器呼叫此批次檔即可。

雲端/虛擬化環境特別注意

  • AWS EC2 / Azure VM / GCP: 建議使用雲端原生排程(如 AWS Instance Scheduler、Azure Automation)而非 Guest OS 內部排程,支援啟動/停止而非重啟,節省成本。
  • 容器: 不應重啟 Host,而應透過編排工具滾動更新容器。
  • 叢集/高可用: 單節點重啟前需確認容錯移轉正常、資料同步完成。

總結

「每月一次、凌晨三點半、shutdown -r -f」是簡單可靠的伺服器維護節奏。搭配服務優雅停止腳本與日誌記錄,能最大程度減少業務影響,保持系統長期穩定運行。

Copyright © Windows教程網 All Rights Reserved