get system boot time in minutes in ruby

You can use the sys/proctable gem to get the system boot time and calculate the uptime in minutes. Here's how you can do it:

main.rb
require 'sys/proctable'

boot_time = Sys::ProcTable.ps(pid: 1).start_time
uptime_minutes = (Time.now - boot_time) / 60

puts "System has been up for #{uptime_minutes.round(2)} minutes."
186 chars
7 lines

In this code, we first require the sys/proctable gem which provides a way to get information about system processes. We then use the ps method to get information about the process with pid 1 which is always the init process and has been running since the system was booted.

We retrieve the start_time of the init process as a Time object and calculate the difference between the current time and the boot time in minutes. Finally, we print the uptime in minutes with two digits after the decimal point using the round method.

gistlibby LogSnag