Object
The Uptime class encapsulates various bits of information regarding your system’s uptime, including boot time.
The Uptime class encapsulates various bits of information regarding your system’s uptime, including boot time.
The version of the sys-uptime library
Returns a Time object indicating the time the system was last booted.
Example:
Sys::Uptime.boot_time # => Mon Jul 13 06:08:25 -0600 2009
# File lib/unix/sys/uptime.rb, line 95 def self.boot_time if RbConfig::CONFIG['host_os'] =~ /linux/ Time.now - self.seconds elsif respond_to?(:sysctl, true) tv = Timeval.new mib = FFI::MemoryPointer.new(:int, 2).write_array_of_int([CTL_KERN, KERN_BOOTTIME]) size = FFI::MemoryPointer.new(:long, 1).write_int(tv.size) if sysctl(mib, 2, tv, size, nil, 0) != 0 raise SystemCallError, 'sysctl() - ' + strerror(FFI.errno) end Time.at(tv[:tv_sec], tv[:tv_usec]) else begin setutxent() while ent = Utmpx.new(getutxent()) if ent[:ut_type] == BOOT_TIME time = Time.at(ent[:ut_tv][:tv_sec], ent[:ut_tv][:tv_usec]) break end end ensure endutxent() end time end end
Returns the total number of days of uptime.
Example:
Sys::Uptime.days # => 2
# File lib/unix/sys/uptime.rb, line 179 def self.days seconds / 86400 end
Returns the uptime as a four element array, including days, hours, minutes and seconds.
Example:
Sys::Uptime.dhms # => [1,9,24,57]
# File lib/unix/sys/uptime.rb, line 209 def self.dhms uptime.split(':') end
Returns the total number of hours of uptime.
Example:
Sys::Uptime.hours # => 31
# File lib/unix/sys/uptime.rb, line 169 def self.hours seconds / 3600 end
Returns the total number of minutes of uptime.
Example:
Sys::Uptime.minutes # => 678
# File lib/unix/sys/uptime.rb, line 159 def self.minutes seconds / 60 end
Returns the total number of seconds of uptime.
Example:
Sys::Uptime.seconds => 118800
# File lib/unix/sys/uptime.rb, line 130 def self.seconds if RbConfig::CONFIG['host_os'] =~ /linux/ begin IO.read('/proc/uptime').split.first.to_i rescue Exception => err raise Error, err end elsif respond_to?(:sysctl, true) tv = Timeval.new mib = FFI::MemoryPointer.new(:int, 2).write_array_of_int([CTL_KERN, KERN_BOOTTIME]) size = FFI::MemoryPointer.new(:long, 1).write_int(tv.size) if sysctl(mib, 2, tv, size, nil, 0) != 0 raise SystemCallError, 'sysctl() - ' + strerror(FFI.errno) end time(nil) - tv[:tv_sec] else tms = Tms.new times(tms) / TICKS end end
Returns the uptime as a colon separated string, including days, hours, minutes and seconds.
Example:
Sys::Uptime.uptime # => "1:9:24:57"
# File lib/unix/sys/uptime.rb, line 190 def self.uptime secs = seconds days = secs / 86400 secs -= days * 86400 hours = secs / 3600 secs -= hours * 3600 mins = secs / 60 secs -= mins * 60 "#{days}:#{hours}:#{mins}:#{secs}" end
Generated with the Darkfish Rdoc Generator 2.