Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the elapsed time that a Rails server has been running for?

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.

like image 233
XåpplI'-I0llwlg'I - Avatar asked Aug 27 '13 12:08

XåpplI'-I0llwlg'I -


1 Answers

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).

like image 73
Jon Cairns Avatar answered Sep 22 '22 06:09

Jon Cairns