如何在Java中获得UNIX正常运行时间? [英] How to get UNIX uptime in Java?

查看:54
本文介绍了如何在Java中获得UNIX正常运行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以Java获得UNIX正常运行时间的最佳方法是什么?我是否可以使用标准的Java库/函数,还是应该使用运行时的exec或ProcessBuilder执行正常运行时间"?谢谢

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

推荐答案

您可以阅读/proc/uptime :

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

来自维基百科:

显示自上次重新启动以来系统已开启多长时间:

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.

这篇关于如何在Java中获得UNIX正常运行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆