Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UNIX uptime in Java?

Tags:

java

unix

What is the best way to get the UNIX uptime in Java? Is there a standard Java library/function I could use or should I use Runtime's exec or ProcessBuilder to execute 'uptime'? Thanks

like image 615
Sam Avatar asked Nov 21 '11 08:11

Sam


1 Answers

You can read /proc/uptime:

new Scanner(new FileInputStream("/proc/uptime")).next();
//"1128046.07" on my machine and still counting

From Wikipedia:

Shows how long the system has been on since it was last restarted:

$ cat /proc/uptime
  350735.47 234388.90

The first number is the total number of seconds the system has been up. The second number is how much of that time the machine has spent idle, in seconds.

like image 156
Tomasz Nurkiewicz Avatar answered Sep 28 '22 00:09

Tomasz Nurkiewicz