get system boot time in seconds in ruby

You can use uptime command and parse the result to get the system boot time. Here's an example:

main.rb
def system_boot_time_seconds
  output = `uptime`
  uptime_parts = output.split('up ')
  uptime = uptime_parts[1].split(',').first.strip
  parts = uptime.split(':').map(&:to_i)
  seconds = (parts[0] * 60 * 60) + (parts[1] * 60) + parts[2]
  return seconds
end

puts system_boot_time_seconds
290 chars
11 lines

This will output the number of seconds since the system was booted.

related categories

gistlibby LogSnag