一、uptime命令
uptime命令可以查看系統的運行時間和負載
終端輸入uptime 04:03:58 up 10 days, 13:19, 1 user, load average: 0.54, 0.40, 0.20
- 當前時間 04:03:58
- 系統已運行的時間 10 days, 13:19
- 當前在線用戶 1 user
- 平均負載:0.54, 0.40, 0.20,最近1分鍾、5分鍾、15分鍾系統的負載
顯然這樣查出的數據是不能展示給用戶來看的,下面上更友好的顯示運行時間的代碼
二、UptimeUtil
package com.starfast.web.util; import java.io.InputStreamReader; import java.io.LineNumberReader; /** * 獲取設備運行時間 * * @author DUCHONG * @since 2018-06-28 14:28 **/ public class UptimeUtil { /** * 獲取linux命令執行的結果,cat 之類 * @param cmd * @return */ public static String getCmdResult(String cmd) { String result = ""; try { Process process = Runtime.getRuntime().exec(cmd); InputStreamReader ir = new InputStreamReader(process.getInputStream()); LineNumberReader input = new LineNumberReader(ir); String line; while ((line = input.readLine()) != null){ result=line; } } catch (java.io.IOException e) { System.err.println("IOException " + e.getMessage()); } return result; } /** * 返回運行時間的秒 * @return */ public static String getUptimeSecond(String str){ String time=null; if(str.contains(",")){ String [] strArr=str.split(","); if(strArr.length>2){ int hms=0; int days=0; if(str.contains("days")){ //截取到天 String day=strArr[0].substring(strArr[0].indexOf("up")+2,strArr[0].indexOf("days")).trim(); //天的秒數 days=Integer.parseInt(day) * 24 * 3600; //時分秒的秒數 hms=Integer.parseInt(getHms(strArr[1].replace("min","").trim())); } else if(str.contains("day")){ //截取到天 String day=strArr[0].substring(strArr[0].indexOf("up")+2,strArr[0].indexOf("day")).trim(); //天的秒數 days=Integer.parseInt(day) * 24 * 3600; //時分秒的秒數 hms=Integer.parseInt(getHms(strArr[1].replace("min","").trim())); } else{ String hmsStr=strArr[0].substring(strArr[0].indexOf("up")+2); hms=Integer.parseInt(getHms(hmsStr.replace("min","").trim())); } Integer totalTime=days+hms; time=totalTime.toString(); } } return time; } /** * 獲取中間字段的秒數 * @param str * @return */ public static String getHms(String str){ String result=null; Integer hms=0; if(str.contains(":")){ String [] hmsArr=str.split("\\:"); int length=hmsArr.length; switch (length){ //只有秒 case 1:hms+=Integer.parseInt(hmsArr[0]); break; //時分 case 2:hms+=(Integer.parseInt(hmsArr[0]) * 3600 + Integer.parseInt(hmsArr[1]) *60); break; //時分秒 case 3:hms+=(Integer.parseInt(hmsArr[0]) * 3600 + Integer.parseInt(hmsArr[1]) *60 +Integer.parseInt(hmsArr[2])); break; } } else{ //不包含: 只能是分 hms+=Integer.parseInt(str) * 60; } if(hms>0){ result=hms.toString(); } return result; } /** * 獲取運行時間 * @return */ public static String getRouterUptime(){ return getUptimeSecond(getCmdResult("uptime")); } public static void main(String[] args) { String s1="14:08:51 up 3 days, 1:04, 2 users, load average: 0.00, 0.00, 0.00"; String s5=" 18:09:13 up 1 day, 1:43, 4 users, load average: 0.51, 0.48, 0.31"; String s2="14:13:34 up 5:06, 4 users, load average: 0.00, 0.01, 0.05"; String s3="16:41:19 up 8 min, 2 users, load average: 0.56, 0.39, 0.17"; String s4="18:03:32 up 1:30, 3 users, load average: 0.06, 0.09, 0.11"; System.out.println(getUptimeSecond(s5)); } }
三、cat /proc/uptime
/** * 返回運行時間的秒-更精確 * @return */ public static String getUptimeSecond2(String str){ String result="0"; if(StringUtils.isNotEmpty(str)) { if (str.contains(" ")) { String[] re = str.split(" "); if (re.length > 0) { String first = re[0]; if (first.contains(".")) { result = first.substring(0, first.indexOf(".")); } else { result = first; } } } else{ if (str.contains(".")) { result = str.substring(0, str.indexOf(".")); } else { result = str; } } } return result; } /** * 獲取系統運行時間-更精確 * @return */ public static String getRouterUptime2(){ return getUptimeSecond2(getCmdResult("cat /proc/uptime")); }