Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

276
Vistas
Getting system uptime in ruby

I am looking for a better way to retrieve a systems uptime. My current method works, but I feel like things can be done better.

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
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

On linux systems you can open /proc/uptime and read seconds since the machine booted.

def started
   Time.now - IO.read('/proc/uptime').split[0].to_f
end 

Edit: changed to_i to to_f. With to_i, the value of started when displayed as a string or integer is more likely to vary, especially if the boot time was close to the middle of a second rather than at the beginning or end of a second.

over 4 years ago · Santiago Trujillo Denunciar

0

You could check the sysinfo gem. It should give system-independent access to the uptime data.

over 4 years ago · Santiago Trujillo Denunciar

0

A rubygem called linux_stat can do that pretty well:

require 'linux_stat'

LinuxStat::OS.uptime
=> {:hour=>40, :minute=>46, :second=>19.75}

Code copied from here.

But if you don't feel like using another gem for this trivial task, you can do this:

uptime = IO.read('/proc/uptime').to_f
uptime_i = uptime.to_i

puts({
    hour: uptime_i / 3600,
    minute: uptime_i % 3600 / 60,
    second: uptime.%(3600).%(60).round(2)
})

Which prints:

{:hour=>0, :minute=>39, :second=>54.89}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda