想追出伺服器「什麼時候被關掉」,Linux 提供了兩條主要線索。
[root@mylnx20 log]# last -x|grep shutdown shutdown system down 2.6.32-504.el6.x Thu Jun 23 17:30 - 09:03 (15:33) shutdown system down 2.6.32-504.el6.x Sun Apr 10 23:29 - 23:29 (00:00) shutdown system down 2.6.32-504.el6.x Sun Apr 10 23:25 - 23:27 (00:02) shutdown system down 2.6.32-504.el6.x Thu Mar 3 00:11 - 00:12 (00:00) shutdown system down 2.6.32-504.el6.x Fri Jun 26 10:50 - 10:50 (00:00)
只取最後一筆就是最近一次關機:
[root@mylnx20 log]# last -x|grep shutdown| head -1 shutdown system down 2.6.32-504.el6.x Thu Jun 23 17:30 - 09:03 (15:33)
順帶一提,把 shutdown 換成 reboot 就能看重啟紀錄。
[root@localhost log]# more /var/log/messages | grep "shutdown" [root@localhost log]# more /var/log/messages.1 | grep "shutdown" Jun 13 11:55:37 localhost shutdown[23387]: shutting down for system halt
第一,messages 會被 logrotate 循環覆蓋,舊紀錄一滾掉就查不到了。第二,某些版本根本沒寫入相關字樣——例如在 Red Hat Enterprise Linux Server release 6.6 上執行 shutdown -h now 之後,messages 裡完全找不到 shutdown 字樣:
[root@cluster-00 log]# more /var/log/messages | grep "acpid: exiting" Jun 24 11:43:49 cluster-00 acpid: exiting
更遗憾的是,這兩種方法都無法告訴你「是哪一台客戶端下達的 shutdown 指令」,只能看到執行時間。若需要追到源頭,得靠 sudo 日誌或額外的稽核(audit)設定。