Check uptime / last restart frontend

Hi.

As an admin with skripting / programming privilege can I check wiki uptime / last restart frontend with a skript or anything else?

Didn’t found something as an extension or snippet with keywords “restart”, “startup” or “uptime”.
Regards, Simpel

Hi, this doesn’t exist. You need to do it from the tool you use to start the Servlet Container containing XWiki. Note that top or ps should provide that info to you. See Five Ways to Check How Long a Process Has Been Running in Linux | 2DayGeek.

So if you have programming rights, nothing prevents you from calling top or ps from inside, say, a Groovy script.

Thank you. I didn’t care a lot about programming rights until now so I was not aware of this way.

I made a groovy script. I’m not sure whether this is good style but it works. Maybe someone has some suggestions for improvement.

This Groovy script determines the time of the last restart of the wiki. Editors of this script need "Programming Right".

{{groovy wiki="false"}}
  import java.text.SimpleDateFormat
  def sout = new StringBuilder(), serr = new StringBuilder()
  // wiki restart means restart of process 'java'
  // so look for pid of 'java'
  def proc = 'pidof java'.execute()
  // get output and error if happening
  proc.consumeProcessOutput(sout, serr)
  // kill command at least after 1 second
  proc.waitForOrKill(1000)
  // if there is an output
  if (sout) {
    // as result has a new line remove it
    sout = sout.deleteCharAt(sout.length()-1)
    // ask for start of the process in long format using the pid catched before
    proc = 'ps -p ' + sout + ' -o lstart'
    proc = proc.execute()
    // reset $sout and $serr
    sout = new StringBuilder()
    serr = new StringBuilder()
    proc.consumeProcessOutput(sout, serr)
    // kill command at least after 1 second
    proc.waitForOrKill(1000)
    // if there is an output
    if (sout) {
      sout = sout.toString()
      // string is like: "                 STARTED\n
      //                  Tue Feb 14 08:01:10 2023\n"
      // use date in line 2 only
      sout = sout.split('\n')[1]
      // the date as seen above has a pattern like this
      def pattern = "EEE MMM dd HH:mm:ss yyyy"
      // make the date string to a real date
      def date = new SimpleDateFormat(pattern).parse(sout)
      // form this date to local german format
      def localDate = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
      // print the result
      print "Last restart: "
      println "$localDate"
    } else {
      println "$serr"
    }
  // if there is only an error
  } else {
    println "$serr"
  }
{{/groovy}}