我正在寻找一种检索系统正常运行时间的更好方法。我目前的方法可行,但我觉得可以做得更好。
def uptime_range
days_up = `uptime | awk {'print$3'}`.chomp.to_i
hours_up = `uptime | awk {'print$5'}`.delete(',').chomp
seconds_up = time_to_seconds(days_up,hours_up)
started = Time.now - seconds_up
"#{started.strftime('%Y.%m.%d.%H.%M')}-#{Time.now.strftime('%Y.%m.%d.%H.%M')}"
end
在linux系统上,您可以打开/ proc / uptime并读取机器启动后的秒数。
def started
Time.now - IO.read('/proc/uptime').split[0].to_f
end
编辑:将to_i更改为to_f。使用to_i,当显示为字符串或整数时,start的值更可能发生变化,尤其是在启动时间接近一秒的中间而不是一秒的开始或结束时。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句