get system boot time in ruby

You can get the system boot time in Ruby by running the following command:

main.rb
uptime = `uptime`.strip
boot_time = Time.now - uptime.match(/up\s+(.*)\d\s*,\s*(\d+\s+:\s+\d+)\s/)[1].to_i * 60
112 chars
3 lines

In the above code, we are first running the uptime command which returns the system's uptime. We then extract the boot time from the output of the uptime command using a regular expression. Finally, we subtract the uptime from the current time to get the system's boot time.

Note that this code will work on Unix-like systems (Linux, macOS, etc.) but may not work on Windows operating systems.

related categories

gistlibby LogSnag