欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

查詢上次Ubuntu重啟時(shí)間的方法命令總結(jié)

 更新時(shí)間:2024年05月23日 10:22:11   作者:wljslmz  
在大多數(shù)情況下,Linux 系統(tǒng)的關(guān)機(jī)時(shí)間、重啟日期和運(yùn)行時(shí)長(zhǎng)等調(diào)試信息在系統(tǒng)故障排錯(cuò)時(shí)會(huì)顯得比較重要,本文將詳細(xì)介紹多種方法來查詢上次 Ubuntu 重啟的時(shí)間,并解釋每種方法的背后原理,需要的朋友可以參考下

引言

在 Ubuntu 系統(tǒng)中,有時(shí)我們需要了解系統(tǒng)上次重啟的日期和時(shí)間。這在系統(tǒng)管理、故障排除和日志審計(jì)中尤為重要。本文將詳細(xì)介紹多種方法來查詢上次 Ubuntu 重啟的時(shí)間,并解釋每種方法的背后原理。

1. 通過 uptime 命令查詢系統(tǒng)運(yùn)行時(shí)間

uptime 命令可以顯示系統(tǒng)已經(jīng)運(yùn)行的時(shí)間。這是最快速且最簡(jiǎn)單的方法之一。

uptime

uptime 命令的輸出通常如下所示:

 12:34:56 up 5 days,  4:23,  3 users,  load average: 0.03, 0.02, 0.00
  • 12:34:56:當(dāng)前時(shí)間
  • up 5 days, 4:23:系統(tǒng)已經(jīng)運(yùn)行的時(shí)間,具體為5天4小時(shí)23分鐘
  • 3 users:當(dāng)前登錄的用戶數(shù)量
  • load average: 0.03, 0.02, 0.00:系統(tǒng)的負(fù)載平均值

通過減去系統(tǒng)運(yùn)行時(shí)間,可以推算出系統(tǒng)的重啟時(shí)間。

假設(shè)當(dāng)前時(shí)間為 2024-05-21 12:34:56,系統(tǒng)已經(jīng)運(yùn)行 5 days, 4:23。

重啟時(shí)間為:2024-05-21 12:34:56 - 5 days 4:23。

使用 Python 腳本計(jì)算:

from datetime import datetime, timedelta

current_time = datetime.strptime("2024-05-21 12:34:56", "%Y-%m-%d %H:%M:%S")
uptime = timedelta(days=5, hours=4, minutes=23)

reboot_time = current_time - uptime
print("Reboot time:", reboot_time)

輸出結(jié)果:

Reboot time: 2024-05-16 08:11:56

2. 使用 last 命令查看系統(tǒng)重啟日志

last 命令可以顯示最近的登錄和重啟事件。

last reboot
reboot   system boot  5.4.0-104-generic Fri May 16 08:11 - 12:34  (5+04:23)
  • reboot:事件類型
  • system boot:系統(tǒng)啟動(dòng)
  • 5.4.0-104-generic:內(nèi)核版本
  • Fri May 16 08:11:重啟時(shí)間
  • 12:34:當(dāng)前時(shí)間
  • (5+04:23):系統(tǒng)運(yùn)行時(shí)間

3. 通過 who 命令檢查系統(tǒng)啟動(dòng)時(shí)間

who 命令帶有 -b 選項(xiàng)可以顯示系統(tǒng)的啟動(dòng)時(shí)間。

who -b
system boot  2024-05-16 08:11
  • system boot:系統(tǒng)啟動(dòng)
  • 2024-05-16 08:11:系統(tǒng)啟動(dòng)時(shí)間

4. 查看系統(tǒng)日志文件獲取重啟時(shí)間

系統(tǒng)日志文件中也記錄了系統(tǒng)的啟動(dòng)和重啟信息。

journalctl --boot
-- Logs begin at Fri 2024-05-16 08:11:34 UTC, end at Fri 2024-05-21 12:34:56 UTC. --
May 16 08:11:34 hostname kernel: Linux version 5.4.0-104-generic (buildd@lgw01-amd64-058) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #117-Ubuntu SMP Fri Apr 16 02:36:12 UTC 2024 (Ubuntu 5.4.0-104.117-generic 5.4.105)

5. 使用 systemd 工具查詢重啟時(shí)間

systemd-analyze 命令可以顯示系統(tǒng)的啟動(dòng)時(shí)間。

systemd-analyze
Startup finished in 1.234s (kernel) + 2.345s (userspace) = 3.579s 
graphical.target reached after 2.456s in userspace
  • 1.234s (kernel):內(nèi)核啟動(dòng)時(shí)間
  • 2.345s (userspace):用戶空間啟動(dòng)時(shí)間
  • 3.579s:總啟動(dòng)時(shí)間

6. 編寫腳本自動(dòng)記錄和查詢重啟時(shí)間

可以編寫腳本自動(dòng)記錄重啟時(shí)間,便于查詢。

#!/bin/bash

logfile="/var/log/reboot_time.log"
if [[ ! -f $logfile ]]; then
  sudo touch $logfile
  sudo chmod 666 $logfile
fi

reboot_time=$(who -b | awk '{print $3, $4}')
echo "Last reboot time: $reboot_time" | sudo tee -a $logfile

7. 使用圖形界面工具查詢重啟時(shí)間

對(duì)于不習(xí)慣使用命令行的用戶,可以使用圖形界面工具。

  • 打開 GNOME 系統(tǒng)監(jiān)視器。
  • 導(dǎo)航到“資源”選項(xiàng)卡。
  • 在“系統(tǒng)”部分查看“啟動(dòng)時(shí)間”。

到此這篇關(guān)于查詢上次Ubuntu重啟時(shí)間的方法命令總結(jié)的文章就介紹到這了,更多相關(guān)查詢Ubuntu重啟時(shí)間內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論