Question

I'd just like to print it out in a HTML view somewhere in my Rails project, along with a bunch of other helpful info. Something like "Current up time = 4d 11h 54m 2s" or "Up since 23rd Aug 2013".

Couldn't find any info on this anywhere, surprisingly. Maybe there's a generic Rack way of doing it.

Was it helpful?

Solution

This is actually pretty easy to do. Create a file, config/initializers/uptime.rb, with:

YourApplication::BOOTED_AT = Time.now

Then it's just a case of comparing the current time to that time, which you can easily do in a helper method, using the time_ago_in_words rails helper:

def uptime
  time_ago_in_words(YourApplication::BOOTED_AT)
end

This gives you a human readable string saying how long the application has been running (give or take a few seconds for the startup).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top