Jenkins uptime - how long since last restart

You can run groovy script on Jenkins web-ui from: Manage Jenkins > Script Console, and use Jenkins API. If you want to know how many days Jenkins has been running:

import java.util.concurrent.TimeUnit
long lastRestarted = Jenkins.instance.toComputer().getConnectTime()
long now =  System.currentTimeMillis()
println TimeUnit.MILLISECONDS.toDays(now - lastRestarted)

getConnectTime() of the master computer should be the time when it restarted. http://javadoc.jenkins.io/hudson/model/Computer.html#getConnectTime()


There has been an Uptime class in Jenkins Core since a long time (Jenkins 1.538). So using the same principle with the script console, the code can be made more readable and robust:

println "Jenkins has been started " + (ExtensionList.lookupSingleton(Uptime.class).uptime / 1000 / 60 ) + " minutes ago"

Which will show, e.g.:

Jenkins has been started 175.8678166667 minutes ago

Note: ExtensionList.lookupSingleton was introduced in Jenkins 2.87. So if you're using an earlier version, use ExtensionList.lookup(Uptime.class).get(0) instead.

Tags:

Jenkins