get system uptime in ruby

You can use the uptime command in the command line to get the system uptime. You can execute this command using Ruby's subprocess module as follows:

main.rb
require 'subprocess'

def system_uptime
  # Execute the `uptime` command and capture its output
  output, _ = Subprocess.check_output(['uptime'])
  
  # Extract the uptime value from the output
  uptime = output.strip.split.last
  
  return uptime
end

puts system_uptime
272 chars
14 lines

This will output the system uptime in the format of days, hours, and minutes. For example: "7 days, 3:12"

gistlibby LogSnag