你可以使用下面的工具来查看 Linux 或类 Unix 系统运行了多长时间:
uptime : 告诉你服务器运行了多长的时间。
lastt : 显示重启和关机时间。
tuptime : 报告系统的运行时间历史和运行时间统计,这是指重启之间的运行时间。和 uptime 命令类似,不过输出结果更有意思。
找出系统上次重启时间和日期
### 显示系统重启和关机时间
who-b
lastreboot
lastshutdown
### 开机信息
uptime
cat/proc/uptime
awk'{ print "up " $1 /60 " minutes"}'/proc/uptime
w
样例输出:
图01:用于找出服务器开机时间的多个Linux命令
跟 tuptime 问打个招呼吧
tuptime 命令行工具可以报告基于 Linux 的系统上的下列信息:
系统启动次数统计
注册首次启动时间(也就是安装时间)
正常关机和意外关机统计
平均开机时间和故障停机时间
当前开机时间
首次启动以来的开机和故障停机率
累积系统开机时间、故障停机时间和合计
报告每次启动、开机时间、关机和故障停机时间
安装
$cd/tmp
$gitclonehttps://github.com/rfrail3/tuptime.git
$ls
$cdtuptime
$ls
样例输出:
图02:克隆git仓库
确保你安装了带有 sys,optparse,os,re,string,sqlite3,datetime,disutils 和 locale 模块的 Python v2.7。
你可以像下面这样来安装:
$sudotuptime-install.sh
或者,可以手工安装(基于 systemd 或非 systemd ):
$sudocp/tmp/tuptime/latest/cron.d/tuptime/etc/cron.d/tuptime
如果系统是systemd的,拷贝服务文件并启用:
$sudocp/tmp/tuptime/latest/systemd/tuptime.service/lib/systemd/system/
$sudosystemctlenable tuptime.service
如果系统不是systemd的,拷贝初始化文件:
$sudocp/tmp/tuptime/latest/init.d/tuptime.init.d-debian7/etc/init.d/tuptime
$sudoupdate-rc.d tuptime defaults
运行
只需输入以下命令:
$sudotuptime
样例输出:
图03:tuptime工作中
在一次更新内核后,我重启了系统,然后再次输入了同样的命令:
$sudotuptime
Systemstartups:2since03:52:16PM08/21/2015
Systemshutdowns:1ok-0bad
Averageuptime:7days,16hours,48minutesand3seconds
Averagedowntime:2hours,30minutesand5seconds
Currentuptime:5minutesand28seconds since06:23:06AM09/06/2015
Uptimerate:98.66%
Downtimerate:1.34%
Systemuptime:15days,9hours,36minutesand7seconds
Systemdowntime:5hours,0minutesand11seconds
Systemlife:15days,14hours,36minutesand18seconds
你可以像下面这样修改日期和时间格式:
$sudotuptime-d'%H:%M:%S %m-%d-%Y'
样例输出:
Systemstartups:1since15:52:1608-21-2015
Systemshutdowns:0ok-0bad
Averageuptime:15days,9hours,21minutesand19seconds
Averagedowntime:0seconds
Currentuptime:15days,9hours,21minutesand19seconds since15:52:1608-21-2015
Uptimerate:100.0%
Downtimerate:0.0%
Systemuptime:15days,9hours,21minutesand19seconds
Systemdowntime:0seconds
Systemlife:15days,9hours,21minutesand19seconds
计算每次启动、开机时间、关机和故障停机时间:
$sudotuptime-e
样例输出:
Startup:1at03:52:16PM08/21/2015
Uptime:15days,9hours,22minutesand33seconds
Systemstartups:1since03:52:16PM08/21/2015
Systemshutdowns:0ok-0bad
Averageuptime:15days,9hours,22minutesand33seconds
Averagedowntime:0seconds
Currentuptime:15days,9hours,22minutesand33seconds since03:52:16PM08/21/2015
Uptimerate:100.0%
Downtimerate:0.0%
Systemuptime:15days,9hours,22minutesand33seconds
Systemdowntime:0seconds
Systemlife:15days,9hours,22minutesand33seconds
作者:Vivek Gite 译者:GOLinux 校对:wxy
本文由 LCTT 原创编译,Linux中国 荣誉推出