一行命令查看Linux系統(tǒng)重啟時(shí)間和相關(guān)記錄。
系統(tǒng)啟動(dòng)時(shí)間并轉(zhuǎn)換為秒:
date -d "$(who -b | awk -F' ' '{print $(NF-1),$NF}')" +%s
當(dāng)前時(shí)間并轉(zhuǎn)換為秒:
date +%s
使用當(dāng)前時(shí)間減去啟動(dòng)時(shí)間,可以得到系統(tǒng)的運(yùn)行時(shí)間。
舉例:假如系統(tǒng)重啟后,10分鐘內(nèi),判斷為系統(tǒng)重啟,否則判斷為運(yùn)行狀態(tài)。很多服務(wù)和應(yīng)用可以根據(jù)系統(tǒng)重啟后,進(jìn)行相應(yīng)的初始化操作。判斷腳本如下:
result=$(uptime | grep min)
if [ "$result" ];then
result=$(echo $result | awk -F' ' '{print $3}')
if [ $result -lt 10 ];then
echo "reboot"
else
echo "running"
fi
else
echo "running"
fi
更多獲取Linux系統(tǒng)運(yùn)行記錄的方法如下:
查看linux的重啟記錄:
last | grep reboot
與之類似可以查看Linux的關(guān)機(jī)記錄:
last | grep shutdown
1、who -b命令 查看最后一次(上次)系統(tǒng)啟動(dòng)的時(shí)間
[root@cy_centos7 ~]# who -b
system boot 2020-06-12 17:44
2、who -r命令 查看最后一次(上次)系統(tǒng)啟動(dòng)的時(shí)間,及運(yùn)行級(jí)別
[root@cy_centos7 ~]# who -r
run-level 5 2020-06-12 17:44
3、last reboot命令
[root@cy_centos7 ~]# last reboot
reboot system boot 3.10.0-957.12.2. Fri Jun 12 17:44 - 10:21 (20+16:37)
reboot system boot 3.10.0-957.12.2. Fri Jun 12 09:28 - 10:21 (21+00:53)
reboot system boot 3.10.0-957.12.2. Mon Jun 8 15:27 - 16:02 (00:35)
reboot system boot 3.10.0-957.12.2. Wed May 27 11:41 - 20:49 (09:08)
reboot system boot 3.10.0-957.12.2. Tue May 19 09:52 - 20:49 (8+10:57)
reboot system boot 3.10.0-957.12.2. Fri May 15 15:42 - 20:04 (04:21)
reboot system boot 3.10.0-957.12.2. Tue May 12 23:01 - 20:04 (2+21:02)
reboot system boot 3.10.0-957.12.2. Fri May 8 21:30 - 10:40 (13:09)
reboot system boot 3.10.0-957.12.2. Wed Apr 22 23:29 - 07:43 (08:13)
...
reboot system boot 3.10.0-957.el7.x Tue May 21 01:49 - 22:30 (1+20:41)
reboot system boot 3.10.0-957.el7.x Tue May 14 19:20 - 22:30 (8+03:10)
reboot system boot 3.10.0-957.el7.x Fri May 10 17:38 - 22:30 (12+04:52)
reboot system boot 3.10.0-957.el7.x Fri May 10 07:19 - 22:30 (12+15:10)
reboot system boot 3.10.0-957.el7.x Fri May 10 07:09 - 22:30 (12+15:21)
reboot system boot 3.10.0-957.el7.x Fri May 10 06:50 - 22:30 (12+15:40)
wtmp begins Fri May 10 06:50:03 2019
4、top命令
top - 10:24:02 up 1 day, 38 min, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 181 total, 1 running, 180 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.6 us, 0.0 sy, 0.0 ni, 98.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0
KiB Mem : 3861508 total, 2981100 free, 424308 used, 456100 buff/cach
KiB Swap: 2097148 total, 2097148 free, 0 used. 3127716 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+
1 root 20 0 191256 4152 2600 S 0.0 0.1 0:06.77
2 root 20 0 0 0 0 S 0.0 0.0 0:00.08
3 root 20 0 0 0 0 S 0.0 0.0 0:01.44
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00
7 root rt 0 0 0 0 S 0.0 0.0 0:00.27
5、w命令 up后表示系統(tǒng)到目前運(yùn)行了多久時(shí)間。反過來推算系統(tǒng)重啟時(shí)間文章來源:http://www.zghlxwxcb.cn/news/detail-444865.html
10:24:35 up 1 day, 38 min, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.100.1 10:20 3.00s 0.03s 0.00s w
6、uptime命令及查看/proc/uptime文章來源地址http://www.zghlxwxcb.cn/news/detail-444865.html
[root@cy_centos7 ~]# uptime
10:25:11 up 1 day, 39 min, 1 user, load average: 0.00, 0.01, 0.05
[root@cy_centos7 ~]# date -d "`cut -f1 -d. /proc/uptime` seconds ago"
Thu Jul 2 09:45:44 CST 2020
[root@cy_centos7 ~]# date -d "$(awk -F. '{print $1}' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"
2020-07-02 09:45:44
到了這里,關(guān)于一行命令查看Linux系統(tǒng)重啟時(shí)間和相關(guān)記錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!