我需要记录电脑的正常运行时间。我怎样才能做到这一点?
我用uptime
这个,当 ubuntu 关闭时我会将此命令输出写入文件中。
答案1
如果您使用的是 Ubuntu
然后在“/etc/rc0.d/S60umountroot”的开头添加以下行。
Log="/var/log/uptime.log"
echo "$(date) $(/usr/bin/uptime)" >> "${Log}"
或者你可以简单地使用logger
logger "UPTIME: $(uptime)"
然后它会在/var/log/syslog
或中提供详细信息/var/log/messages
注意:- 请小心编辑此文件
如果您使用的是 CentOs,则在“/etc/rc.d/rc0.d/S01halt”中执行相同的操作,但请注意,应将其添加到文件的开头。
答案2
如果您需要跟踪系统正常运行时间行为,请尝试启动时间它报告系统的历史和统计运行时间,并在每次重新启动之间保存它。
# tuptime -l
Startup: 1 at 01:30:24 PM 08/10/2015
Uptime: 1 minutes and 24 seconds
Shutdown: OK at 01:31:48 PM 08/10/2015
Downtime: 6 seconds
Startup: 2 at 01:31:55 PM 08/10/2015
Uptime: 3 days, 20 hours, 15 minutes and 35 seconds
System startups: 2 since 01:30:24 PM 08/10/2015
System shutdowns: 1 ok - 0 bad
Average uptime: 1 days, 22 hours, 8 minutes and 30 seconds
Average downtime: 3 seconds
Current uptime: 3 days, 20 hours, 15 minutes and 35 seconds since 01:31:55 PM 08/10/2015
Uptime rate: 100.0 %
Downtime rate: 0.0 %
System uptime: 3 days, 20 hours, 17 minutes and 0 seconds
System downtime: 6 seconds
System life: 3 days, 20 hours, 17 minutes and 6 seconds