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?
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.
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.
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.
You can use RuntimeMXBean.getUptime()
RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
long uptime = rb.getUptime();
The result is in milliseconds:
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
System.out.println(mxBean.getUptime());
See: ManagementFactory.getRuntimeMXBean() javadoc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With