Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get application uptime

Tags:

I've written an application in java and I want to add a feature to report the uptime of the application. Is there a Class/method in the JVM that can do this?

Should I save the timestamp when application starts, then calculate the difference with the current uptime in the moment of the request?

What's a better way to retrieve application uptime in Java?

like image 1000
aleroot Avatar asked Jun 21 '11 20:06

aleroot


People also ask

How do you find the uptime of a process?

Process Uptime ps helps us to see a snapshot of current processes in the operating system. ps stands for “Process Status” Using -p <process_id> option we can specify the process id. For example, -p20 or -p 20 are representing a process with 20 as the process id.

What is uptime command?

The uptime command prints the current time, the length of time the system has been up, the number of users online, and the load average. The load average is the number of runnable processes over the preceding 1-, 5-, 15-minute intervals.

How do I see uptime in Linux?

First, open the terminal window and then type: uptime command – Tell how long the Linux system has been running. w command – Show who is logged on and what they are doing including the uptime of a Linux box. top command – Display Linux server processes and display system Uptime in Linux too.


2 Answers

You can use RuntimeMXBean.getUptime()

RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
long uptime = rb.getUptime();
like image 85
Rob Harrop Avatar answered Sep 25 '22 07:09

Rob Harrop


The result is in milliseconds:

RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
System.out.println(mxBean.getUptime());

See: ManagementFactory.getRuntimeMXBean() javadoc.

like image 43
Tomasz Nurkiewicz Avatar answered Sep 25 '22 07:09

Tomasz Nurkiewicz