What is uptime in Linux and How to Parse Result in Java using Regular Expression (RegEx)?

Uptime Result Linux Parse Load Average What is uptime in Linux and How to Parse Result in Java using Regular Expression (RegEx)?

What is uptime in Linux? Sometimes you have to get load averages from uptime result via Java Program to perform certain operations. Like nuke, restart app, etc based on high load average.

I’m talking about Linux uptime Command result. There are multiple Java Management APIs available under RuntimeMXBean class but we will discuss all possibilities about it later. There is a sample example on how to get uptime using RuntimeMXBean which I’ve published sometime back if you want to check. One thing to note here is: uptime result may vary based on platform, i.e. different result for Mac, Linux, Unix, etc.

Let’s focus on Linux based uptime result only. You may have below questions:

  • How to Parse the output of “uptime” with bash
  • Linux: Getting date & time of system startup using uptime
  • Correctly grep and display the uptime, load average
  • uptime – Linux: Getting date & time of system startup
  • Parse Linux Uptime result

Here is a sample result of uptime command:

Now in this Java example we will follow below steps:

  1. Create class CrunchifyGetUptimeResultAndParse.java
  2. Execute uptime command via ProcessBuilder Java API
  3. Get result into uptimeCmdResult variable
  4. Apply Java Regex (Pattern matching) operation on variable uptimeCmdResult to get 1 min, 5 min, 15 min load average

Here is a Pattern:

Result:

Bonus Regex Operations for uptime result in Mac:

Just returns number of users:

Just returns mins

Have anything to add to this article? Please chime in and join the conversion.
twitter What is uptime in Linux and How to Parse Result in Java using Regular Expression (RegEx)?facebook What is uptime in Linux and How to Parse Result in Java using Regular Expression (RegEx)?google What is uptime in Linux and How to Parse Result in Java using Regular Expression (RegEx)?pinterest What is uptime in Linux and How to Parse Result in Java using Regular Expression (RegEx)?tumblr What is uptime in Linux and How to Parse Result in Java using Regular Expression (RegEx)?buffer What is uptime in Linux and How to Parse Result in Java using Regular Expression (RegEx)?